bmc150-accel-core.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  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. #include <linux/regmap.h>
  38. #include "bmc150-accel.h"
  39. #define BMC150_ACCEL_DRV_NAME "bmc150_accel"
  40. #define BMC150_ACCEL_IRQ_NAME "bmc150_accel_event"
  41. #define BMC150_ACCEL_REG_CHIP_ID 0x00
  42. #define BMC150_ACCEL_REG_INT_STATUS_2 0x0B
  43. #define BMC150_ACCEL_ANY_MOTION_MASK 0x07
  44. #define BMC150_ACCEL_ANY_MOTION_BIT_X BIT(0)
  45. #define BMC150_ACCEL_ANY_MOTION_BIT_Y BIT(1)
  46. #define BMC150_ACCEL_ANY_MOTION_BIT_Z BIT(2)
  47. #define BMC150_ACCEL_ANY_MOTION_BIT_SIGN BIT(3)
  48. #define BMC150_ACCEL_REG_PMU_LPW 0x11
  49. #define BMC150_ACCEL_PMU_MODE_MASK 0xE0
  50. #define BMC150_ACCEL_PMU_MODE_SHIFT 5
  51. #define BMC150_ACCEL_PMU_BIT_SLEEP_DUR_MASK 0x17
  52. #define BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT 1
  53. #define BMC150_ACCEL_REG_PMU_RANGE 0x0F
  54. #define BMC150_ACCEL_DEF_RANGE_2G 0x03
  55. #define BMC150_ACCEL_DEF_RANGE_4G 0x05
  56. #define BMC150_ACCEL_DEF_RANGE_8G 0x08
  57. #define BMC150_ACCEL_DEF_RANGE_16G 0x0C
  58. /* Default BW: 125Hz */
  59. #define BMC150_ACCEL_REG_PMU_BW 0x10
  60. #define BMC150_ACCEL_DEF_BW 125
  61. #define BMC150_ACCEL_REG_RESET 0x14
  62. #define BMC150_ACCEL_RESET_VAL 0xB6
  63. #define BMC150_ACCEL_REG_INT_MAP_0 0x19
  64. #define BMC150_ACCEL_INT_MAP_0_BIT_SLOPE BIT(2)
  65. #define BMC150_ACCEL_REG_INT_MAP_1 0x1A
  66. #define BMC150_ACCEL_INT_MAP_1_BIT_DATA BIT(0)
  67. #define BMC150_ACCEL_INT_MAP_1_BIT_FWM BIT(1)
  68. #define BMC150_ACCEL_INT_MAP_1_BIT_FFULL BIT(2)
  69. #define BMC150_ACCEL_REG_INT_RST_LATCH 0x21
  70. #define BMC150_ACCEL_INT_MODE_LATCH_RESET 0x80
  71. #define BMC150_ACCEL_INT_MODE_LATCH_INT 0x0F
  72. #define BMC150_ACCEL_INT_MODE_NON_LATCH_INT 0x00
  73. #define BMC150_ACCEL_REG_INT_EN_0 0x16
  74. #define BMC150_ACCEL_INT_EN_BIT_SLP_X BIT(0)
  75. #define BMC150_ACCEL_INT_EN_BIT_SLP_Y BIT(1)
  76. #define BMC150_ACCEL_INT_EN_BIT_SLP_Z BIT(2)
  77. #define BMC150_ACCEL_REG_INT_EN_1 0x17
  78. #define BMC150_ACCEL_INT_EN_BIT_DATA_EN BIT(4)
  79. #define BMC150_ACCEL_INT_EN_BIT_FFULL_EN BIT(5)
  80. #define BMC150_ACCEL_INT_EN_BIT_FWM_EN BIT(6)
  81. #define BMC150_ACCEL_REG_INT_OUT_CTRL 0x20
  82. #define BMC150_ACCEL_INT_OUT_CTRL_INT1_LVL BIT(0)
  83. #define BMC150_ACCEL_REG_INT_5 0x27
  84. #define BMC150_ACCEL_SLOPE_DUR_MASK 0x03
  85. #define BMC150_ACCEL_REG_INT_6 0x28
  86. #define BMC150_ACCEL_SLOPE_THRES_MASK 0xFF
  87. /* Slope duration in terms of number of samples */
  88. #define BMC150_ACCEL_DEF_SLOPE_DURATION 1
  89. /* in terms of multiples of g's/LSB, based on range */
  90. #define BMC150_ACCEL_DEF_SLOPE_THRESHOLD 1
  91. #define BMC150_ACCEL_REG_XOUT_L 0x02
  92. #define BMC150_ACCEL_MAX_STARTUP_TIME_MS 100
  93. /* Sleep Duration values */
  94. #define BMC150_ACCEL_SLEEP_500_MICRO 0x05
  95. #define BMC150_ACCEL_SLEEP_1_MS 0x06
  96. #define BMC150_ACCEL_SLEEP_2_MS 0x07
  97. #define BMC150_ACCEL_SLEEP_4_MS 0x08
  98. #define BMC150_ACCEL_SLEEP_6_MS 0x09
  99. #define BMC150_ACCEL_SLEEP_10_MS 0x0A
  100. #define BMC150_ACCEL_SLEEP_25_MS 0x0B
  101. #define BMC150_ACCEL_SLEEP_50_MS 0x0C
  102. #define BMC150_ACCEL_SLEEP_100_MS 0x0D
  103. #define BMC150_ACCEL_SLEEP_500_MS 0x0E
  104. #define BMC150_ACCEL_SLEEP_1_SEC 0x0F
  105. #define BMC150_ACCEL_REG_TEMP 0x08
  106. #define BMC150_ACCEL_TEMP_CENTER_VAL 24
  107. #define BMC150_ACCEL_AXIS_TO_REG(axis) (BMC150_ACCEL_REG_XOUT_L + (axis * 2))
  108. #define BMC150_AUTO_SUSPEND_DELAY_MS 2000
  109. #define BMC150_ACCEL_REG_FIFO_STATUS 0x0E
  110. #define BMC150_ACCEL_REG_FIFO_CONFIG0 0x30
  111. #define BMC150_ACCEL_REG_FIFO_CONFIG1 0x3E
  112. #define BMC150_ACCEL_REG_FIFO_DATA 0x3F
  113. #define BMC150_ACCEL_FIFO_LENGTH 32
  114. enum bmc150_accel_axis {
  115. AXIS_X,
  116. AXIS_Y,
  117. AXIS_Z,
  118. };
  119. enum bmc150_power_modes {
  120. BMC150_ACCEL_SLEEP_MODE_NORMAL,
  121. BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND,
  122. BMC150_ACCEL_SLEEP_MODE_LPM,
  123. BMC150_ACCEL_SLEEP_MODE_SUSPEND = 0x04,
  124. };
  125. struct bmc150_scale_info {
  126. int scale;
  127. u8 reg_range;
  128. };
  129. struct bmc150_accel_chip_info {
  130. const char *name;
  131. u8 chip_id;
  132. const struct iio_chan_spec *channels;
  133. int num_channels;
  134. const struct bmc150_scale_info scale_table[4];
  135. };
  136. struct bmc150_accel_interrupt {
  137. const struct bmc150_accel_interrupt_info *info;
  138. atomic_t users;
  139. };
  140. struct bmc150_accel_trigger {
  141. struct bmc150_accel_data *data;
  142. struct iio_trigger *indio_trig;
  143. int (*setup)(struct bmc150_accel_trigger *t, bool state);
  144. int intr;
  145. bool enabled;
  146. };
  147. enum bmc150_accel_interrupt_id {
  148. BMC150_ACCEL_INT_DATA_READY,
  149. BMC150_ACCEL_INT_ANY_MOTION,
  150. BMC150_ACCEL_INT_WATERMARK,
  151. BMC150_ACCEL_INTERRUPTS,
  152. };
  153. enum bmc150_accel_trigger_id {
  154. BMC150_ACCEL_TRIGGER_DATA_READY,
  155. BMC150_ACCEL_TRIGGER_ANY_MOTION,
  156. BMC150_ACCEL_TRIGGERS,
  157. };
  158. struct bmc150_accel_data {
  159. struct regmap *regmap;
  160. struct device *dev;
  161. int irq;
  162. struct bmc150_accel_interrupt interrupts[BMC150_ACCEL_INTERRUPTS];
  163. struct bmc150_accel_trigger triggers[BMC150_ACCEL_TRIGGERS];
  164. struct mutex mutex;
  165. u8 fifo_mode, watermark;
  166. s16 buffer[8];
  167. u8 bw_bits;
  168. u32 slope_dur;
  169. u32 slope_thres;
  170. u32 range;
  171. int ev_enable_state;
  172. int64_t timestamp, old_timestamp; /* Only used in hw fifo mode. */
  173. const struct bmc150_accel_chip_info *chip_info;
  174. };
  175. static const struct {
  176. int val;
  177. int val2;
  178. u8 bw_bits;
  179. } bmc150_accel_samp_freq_table[] = { {15, 620000, 0x08},
  180. {31, 260000, 0x09},
  181. {62, 500000, 0x0A},
  182. {125, 0, 0x0B},
  183. {250, 0, 0x0C},
  184. {500, 0, 0x0D},
  185. {1000, 0, 0x0E},
  186. {2000, 0, 0x0F} };
  187. static const struct {
  188. int bw_bits;
  189. int msec;
  190. } bmc150_accel_sample_upd_time[] = { {0x08, 64},
  191. {0x09, 32},
  192. {0x0A, 16},
  193. {0x0B, 8},
  194. {0x0C, 4},
  195. {0x0D, 2},
  196. {0x0E, 1},
  197. {0x0F, 1} };
  198. static const struct {
  199. int sleep_dur;
  200. u8 reg_value;
  201. } bmc150_accel_sleep_value_table[] = { {0, 0},
  202. {500, BMC150_ACCEL_SLEEP_500_MICRO},
  203. {1000, BMC150_ACCEL_SLEEP_1_MS},
  204. {2000, BMC150_ACCEL_SLEEP_2_MS},
  205. {4000, BMC150_ACCEL_SLEEP_4_MS},
  206. {6000, BMC150_ACCEL_SLEEP_6_MS},
  207. {10000, BMC150_ACCEL_SLEEP_10_MS},
  208. {25000, BMC150_ACCEL_SLEEP_25_MS},
  209. {50000, BMC150_ACCEL_SLEEP_50_MS},
  210. {100000, BMC150_ACCEL_SLEEP_100_MS},
  211. {500000, BMC150_ACCEL_SLEEP_500_MS},
  212. {1000000, BMC150_ACCEL_SLEEP_1_SEC} };
  213. static const struct regmap_config bmc150_i2c_regmap_conf = {
  214. .reg_bits = 8,
  215. .val_bits = 8,
  216. .max_register = 0x3f,
  217. };
  218. static int bmc150_accel_set_mode(struct bmc150_accel_data *data,
  219. enum bmc150_power_modes mode,
  220. int dur_us)
  221. {
  222. int i;
  223. int ret;
  224. u8 lpw_bits;
  225. int dur_val = -1;
  226. if (dur_us > 0) {
  227. for (i = 0; i < ARRAY_SIZE(bmc150_accel_sleep_value_table);
  228. ++i) {
  229. if (bmc150_accel_sleep_value_table[i].sleep_dur ==
  230. dur_us)
  231. dur_val =
  232. bmc150_accel_sleep_value_table[i].reg_value;
  233. }
  234. } else {
  235. dur_val = 0;
  236. }
  237. if (dur_val < 0)
  238. return -EINVAL;
  239. lpw_bits = mode << BMC150_ACCEL_PMU_MODE_SHIFT;
  240. lpw_bits |= (dur_val << BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT);
  241. dev_dbg(data->dev, "Set Mode bits %x\n", lpw_bits);
  242. ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_LPW, lpw_bits);
  243. if (ret < 0) {
  244. dev_err(data->dev, "Error writing reg_pmu_lpw\n");
  245. return ret;
  246. }
  247. return 0;
  248. }
  249. static int bmc150_accel_set_bw(struct bmc150_accel_data *data, int val,
  250. int val2)
  251. {
  252. int i;
  253. int ret;
  254. for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) {
  255. if (bmc150_accel_samp_freq_table[i].val == val &&
  256. bmc150_accel_samp_freq_table[i].val2 == val2) {
  257. ret = regmap_write(data->regmap,
  258. BMC150_ACCEL_REG_PMU_BW,
  259. bmc150_accel_samp_freq_table[i].bw_bits);
  260. if (ret < 0)
  261. return ret;
  262. data->bw_bits =
  263. bmc150_accel_samp_freq_table[i].bw_bits;
  264. return 0;
  265. }
  266. }
  267. return -EINVAL;
  268. }
  269. static int bmc150_accel_update_slope(struct bmc150_accel_data *data)
  270. {
  271. int ret;
  272. ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_6,
  273. data->slope_thres);
  274. if (ret < 0) {
  275. dev_err(data->dev, "Error writing reg_int_6\n");
  276. return ret;
  277. }
  278. ret = regmap_update_bits(data->regmap, BMC150_ACCEL_REG_INT_5,
  279. BMC150_ACCEL_SLOPE_DUR_MASK, data->slope_dur);
  280. if (ret < 0) {
  281. dev_err(data->dev, "Error updating reg_int_5\n");
  282. return ret;
  283. }
  284. dev_dbg(data->dev, "%s: %x %x\n", __func__, data->slope_thres,
  285. data->slope_dur);
  286. return ret;
  287. }
  288. static int bmc150_accel_any_motion_setup(struct bmc150_accel_trigger *t,
  289. bool state)
  290. {
  291. if (state)
  292. return bmc150_accel_update_slope(t->data);
  293. return 0;
  294. }
  295. static int bmc150_accel_get_bw(struct bmc150_accel_data *data, int *val,
  296. int *val2)
  297. {
  298. int i;
  299. for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) {
  300. if (bmc150_accel_samp_freq_table[i].bw_bits == data->bw_bits) {
  301. *val = bmc150_accel_samp_freq_table[i].val;
  302. *val2 = bmc150_accel_samp_freq_table[i].val2;
  303. return IIO_VAL_INT_PLUS_MICRO;
  304. }
  305. }
  306. return -EINVAL;
  307. }
  308. #ifdef CONFIG_PM
  309. static int bmc150_accel_get_startup_times(struct bmc150_accel_data *data)
  310. {
  311. int i;
  312. for (i = 0; i < ARRAY_SIZE(bmc150_accel_sample_upd_time); ++i) {
  313. if (bmc150_accel_sample_upd_time[i].bw_bits == data->bw_bits)
  314. return bmc150_accel_sample_upd_time[i].msec;
  315. }
  316. return BMC150_ACCEL_MAX_STARTUP_TIME_MS;
  317. }
  318. static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
  319. {
  320. int ret;
  321. if (on) {
  322. ret = pm_runtime_get_sync(data->dev);
  323. } else {
  324. pm_runtime_mark_last_busy(data->dev);
  325. ret = pm_runtime_put_autosuspend(data->dev);
  326. }
  327. if (ret < 0) {
  328. dev_err(data->dev,
  329. "Failed: bmc150_accel_set_power_state for %d\n", on);
  330. if (on)
  331. pm_runtime_put_noidle(data->dev);
  332. return ret;
  333. }
  334. return 0;
  335. }
  336. #else
  337. static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
  338. {
  339. return 0;
  340. }
  341. #endif
  342. static const struct bmc150_accel_interrupt_info {
  343. u8 map_reg;
  344. u8 map_bitmask;
  345. u8 en_reg;
  346. u8 en_bitmask;
  347. } bmc150_accel_interrupts[BMC150_ACCEL_INTERRUPTS] = {
  348. { /* data ready interrupt */
  349. .map_reg = BMC150_ACCEL_REG_INT_MAP_1,
  350. .map_bitmask = BMC150_ACCEL_INT_MAP_1_BIT_DATA,
  351. .en_reg = BMC150_ACCEL_REG_INT_EN_1,
  352. .en_bitmask = BMC150_ACCEL_INT_EN_BIT_DATA_EN,
  353. },
  354. { /* motion interrupt */
  355. .map_reg = BMC150_ACCEL_REG_INT_MAP_0,
  356. .map_bitmask = BMC150_ACCEL_INT_MAP_0_BIT_SLOPE,
  357. .en_reg = BMC150_ACCEL_REG_INT_EN_0,
  358. .en_bitmask = BMC150_ACCEL_INT_EN_BIT_SLP_X |
  359. BMC150_ACCEL_INT_EN_BIT_SLP_Y |
  360. BMC150_ACCEL_INT_EN_BIT_SLP_Z
  361. },
  362. { /* fifo watermark interrupt */
  363. .map_reg = BMC150_ACCEL_REG_INT_MAP_1,
  364. .map_bitmask = BMC150_ACCEL_INT_MAP_1_BIT_FWM,
  365. .en_reg = BMC150_ACCEL_REG_INT_EN_1,
  366. .en_bitmask = BMC150_ACCEL_INT_EN_BIT_FWM_EN,
  367. },
  368. };
  369. static void bmc150_accel_interrupts_setup(struct iio_dev *indio_dev,
  370. struct bmc150_accel_data *data)
  371. {
  372. int i;
  373. for (i = 0; i < BMC150_ACCEL_INTERRUPTS; i++)
  374. data->interrupts[i].info = &bmc150_accel_interrupts[i];
  375. }
  376. static int bmc150_accel_set_interrupt(struct bmc150_accel_data *data, int i,
  377. bool state)
  378. {
  379. struct bmc150_accel_interrupt *intr = &data->interrupts[i];
  380. const struct bmc150_accel_interrupt_info *info = intr->info;
  381. int ret;
  382. if (state) {
  383. if (atomic_inc_return(&intr->users) > 1)
  384. return 0;
  385. } else {
  386. if (atomic_dec_return(&intr->users) > 0)
  387. return 0;
  388. }
  389. /*
  390. * We will expect the enable and disable to do operation in reverse
  391. * order. This will happen here anyway, as our resume operation uses
  392. * sync mode runtime pm calls. The suspend operation will be delayed
  393. * by autosuspend delay.
  394. * So the disable operation will still happen in reverse order of
  395. * enable operation. When runtime pm is disabled the mode is always on,
  396. * so sequence doesn't matter.
  397. */
  398. ret = bmc150_accel_set_power_state(data, state);
  399. if (ret < 0)
  400. return ret;
  401. /* map the interrupt to the appropriate pins */
  402. ret = regmap_update_bits(data->regmap, info->map_reg, info->map_bitmask,
  403. (state ? info->map_bitmask : 0));
  404. if (ret < 0) {
  405. dev_err(data->dev, "Error updating reg_int_map\n");
  406. goto out_fix_power_state;
  407. }
  408. /* enable/disable the interrupt */
  409. ret = regmap_update_bits(data->regmap, info->en_reg, info->en_bitmask,
  410. (state ? info->en_bitmask : 0));
  411. if (ret < 0) {
  412. dev_err(data->dev, "Error updating reg_int_en\n");
  413. goto out_fix_power_state;
  414. }
  415. return 0;
  416. out_fix_power_state:
  417. bmc150_accel_set_power_state(data, false);
  418. return ret;
  419. }
  420. static int bmc150_accel_set_scale(struct bmc150_accel_data *data, int val)
  421. {
  422. int ret, i;
  423. for (i = 0; i < ARRAY_SIZE(data->chip_info->scale_table); ++i) {
  424. if (data->chip_info->scale_table[i].scale == val) {
  425. ret = regmap_write(data->regmap,
  426. BMC150_ACCEL_REG_PMU_RANGE,
  427. data->chip_info->scale_table[i].reg_range);
  428. if (ret < 0) {
  429. dev_err(data->dev,
  430. "Error writing pmu_range\n");
  431. return ret;
  432. }
  433. data->range = data->chip_info->scale_table[i].reg_range;
  434. return 0;
  435. }
  436. }
  437. return -EINVAL;
  438. }
  439. static int bmc150_accel_get_temp(struct bmc150_accel_data *data, int *val)
  440. {
  441. int ret;
  442. unsigned int value;
  443. mutex_lock(&data->mutex);
  444. ret = regmap_read(data->regmap, BMC150_ACCEL_REG_TEMP, &value);
  445. if (ret < 0) {
  446. dev_err(data->dev, "Error reading reg_temp\n");
  447. mutex_unlock(&data->mutex);
  448. return ret;
  449. }
  450. *val = sign_extend32(value, 7);
  451. mutex_unlock(&data->mutex);
  452. return IIO_VAL_INT;
  453. }
  454. static int bmc150_accel_get_axis(struct bmc150_accel_data *data,
  455. struct iio_chan_spec const *chan,
  456. int *val)
  457. {
  458. int ret;
  459. int axis = chan->scan_index;
  460. __le16 raw_val;
  461. mutex_lock(&data->mutex);
  462. ret = bmc150_accel_set_power_state(data, true);
  463. if (ret < 0) {
  464. mutex_unlock(&data->mutex);
  465. return ret;
  466. }
  467. ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_AXIS_TO_REG(axis),
  468. &raw_val, sizeof(raw_val));
  469. if (ret < 0) {
  470. dev_err(data->dev, "Error reading axis %d\n", axis);
  471. bmc150_accel_set_power_state(data, false);
  472. mutex_unlock(&data->mutex);
  473. return ret;
  474. }
  475. *val = sign_extend32(le16_to_cpu(raw_val) >> chan->scan_type.shift,
  476. chan->scan_type.realbits - 1);
  477. ret = bmc150_accel_set_power_state(data, false);
  478. mutex_unlock(&data->mutex);
  479. if (ret < 0)
  480. return ret;
  481. return IIO_VAL_INT;
  482. }
  483. static int bmc150_accel_read_raw(struct iio_dev *indio_dev,
  484. struct iio_chan_spec const *chan,
  485. int *val, int *val2, long mask)
  486. {
  487. struct bmc150_accel_data *data = iio_priv(indio_dev);
  488. int ret;
  489. switch (mask) {
  490. case IIO_CHAN_INFO_RAW:
  491. switch (chan->type) {
  492. case IIO_TEMP:
  493. return bmc150_accel_get_temp(data, val);
  494. case IIO_ACCEL:
  495. if (iio_buffer_enabled(indio_dev))
  496. return -EBUSY;
  497. else
  498. return bmc150_accel_get_axis(data, chan, val);
  499. default:
  500. return -EINVAL;
  501. }
  502. case IIO_CHAN_INFO_OFFSET:
  503. if (chan->type == IIO_TEMP) {
  504. *val = BMC150_ACCEL_TEMP_CENTER_VAL;
  505. return IIO_VAL_INT;
  506. } else {
  507. return -EINVAL;
  508. }
  509. case IIO_CHAN_INFO_SCALE:
  510. *val = 0;
  511. switch (chan->type) {
  512. case IIO_TEMP:
  513. *val2 = 500000;
  514. return IIO_VAL_INT_PLUS_MICRO;
  515. case IIO_ACCEL:
  516. {
  517. int i;
  518. const struct bmc150_scale_info *si;
  519. int st_size = ARRAY_SIZE(data->chip_info->scale_table);
  520. for (i = 0; i < st_size; ++i) {
  521. si = &data->chip_info->scale_table[i];
  522. if (si->reg_range == data->range) {
  523. *val2 = si->scale;
  524. return IIO_VAL_INT_PLUS_MICRO;
  525. }
  526. }
  527. return -EINVAL;
  528. }
  529. default:
  530. return -EINVAL;
  531. }
  532. case IIO_CHAN_INFO_SAMP_FREQ:
  533. mutex_lock(&data->mutex);
  534. ret = bmc150_accel_get_bw(data, val, val2);
  535. mutex_unlock(&data->mutex);
  536. return ret;
  537. default:
  538. return -EINVAL;
  539. }
  540. }
  541. static int bmc150_accel_write_raw(struct iio_dev *indio_dev,
  542. struct iio_chan_spec const *chan,
  543. int val, int val2, long mask)
  544. {
  545. struct bmc150_accel_data *data = iio_priv(indio_dev);
  546. int ret;
  547. switch (mask) {
  548. case IIO_CHAN_INFO_SAMP_FREQ:
  549. mutex_lock(&data->mutex);
  550. ret = bmc150_accel_set_bw(data, val, val2);
  551. mutex_unlock(&data->mutex);
  552. break;
  553. case IIO_CHAN_INFO_SCALE:
  554. if (val)
  555. return -EINVAL;
  556. mutex_lock(&data->mutex);
  557. ret = bmc150_accel_set_scale(data, val2);
  558. mutex_unlock(&data->mutex);
  559. return ret;
  560. default:
  561. ret = -EINVAL;
  562. }
  563. return ret;
  564. }
  565. static int bmc150_accel_read_event(struct iio_dev *indio_dev,
  566. const struct iio_chan_spec *chan,
  567. enum iio_event_type type,
  568. enum iio_event_direction dir,
  569. enum iio_event_info info,
  570. int *val, int *val2)
  571. {
  572. struct bmc150_accel_data *data = iio_priv(indio_dev);
  573. *val2 = 0;
  574. switch (info) {
  575. case IIO_EV_INFO_VALUE:
  576. *val = data->slope_thres;
  577. break;
  578. case IIO_EV_INFO_PERIOD:
  579. *val = data->slope_dur;
  580. break;
  581. default:
  582. return -EINVAL;
  583. }
  584. return IIO_VAL_INT;
  585. }
  586. static int bmc150_accel_write_event(struct iio_dev *indio_dev,
  587. const struct iio_chan_spec *chan,
  588. enum iio_event_type type,
  589. enum iio_event_direction dir,
  590. enum iio_event_info info,
  591. int val, int val2)
  592. {
  593. struct bmc150_accel_data *data = iio_priv(indio_dev);
  594. if (data->ev_enable_state)
  595. return -EBUSY;
  596. switch (info) {
  597. case IIO_EV_INFO_VALUE:
  598. data->slope_thres = val & BMC150_ACCEL_SLOPE_THRES_MASK;
  599. break;
  600. case IIO_EV_INFO_PERIOD:
  601. data->slope_dur = val & BMC150_ACCEL_SLOPE_DUR_MASK;
  602. break;
  603. default:
  604. return -EINVAL;
  605. }
  606. return 0;
  607. }
  608. static int bmc150_accel_read_event_config(struct iio_dev *indio_dev,
  609. const struct iio_chan_spec *chan,
  610. enum iio_event_type type,
  611. enum iio_event_direction dir)
  612. {
  613. struct bmc150_accel_data *data = iio_priv(indio_dev);
  614. return data->ev_enable_state;
  615. }
  616. static int bmc150_accel_write_event_config(struct iio_dev *indio_dev,
  617. const struct iio_chan_spec *chan,
  618. enum iio_event_type type,
  619. enum iio_event_direction dir,
  620. int state)
  621. {
  622. struct bmc150_accel_data *data = iio_priv(indio_dev);
  623. int ret;
  624. if (state == data->ev_enable_state)
  625. return 0;
  626. mutex_lock(&data->mutex);
  627. ret = bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_ANY_MOTION,
  628. state);
  629. if (ret < 0) {
  630. mutex_unlock(&data->mutex);
  631. return ret;
  632. }
  633. data->ev_enable_state = state;
  634. mutex_unlock(&data->mutex);
  635. return 0;
  636. }
  637. static int bmc150_accel_validate_trigger(struct iio_dev *indio_dev,
  638. struct iio_trigger *trig)
  639. {
  640. struct bmc150_accel_data *data = iio_priv(indio_dev);
  641. int i;
  642. for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) {
  643. if (data->triggers[i].indio_trig == trig)
  644. return 0;
  645. }
  646. return -EINVAL;
  647. }
  648. static ssize_t bmc150_accel_get_fifo_watermark(struct device *dev,
  649. struct device_attribute *attr,
  650. char *buf)
  651. {
  652. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  653. struct bmc150_accel_data *data = iio_priv(indio_dev);
  654. int wm;
  655. mutex_lock(&data->mutex);
  656. wm = data->watermark;
  657. mutex_unlock(&data->mutex);
  658. return sprintf(buf, "%d\n", wm);
  659. }
  660. static ssize_t bmc150_accel_get_fifo_state(struct device *dev,
  661. struct device_attribute *attr,
  662. char *buf)
  663. {
  664. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  665. struct bmc150_accel_data *data = iio_priv(indio_dev);
  666. bool state;
  667. mutex_lock(&data->mutex);
  668. state = data->fifo_mode;
  669. mutex_unlock(&data->mutex);
  670. return sprintf(buf, "%d\n", state);
  671. }
  672. static IIO_CONST_ATTR(hwfifo_watermark_min, "1");
  673. static IIO_CONST_ATTR(hwfifo_watermark_max,
  674. __stringify(BMC150_ACCEL_FIFO_LENGTH));
  675. static IIO_DEVICE_ATTR(hwfifo_enabled, S_IRUGO,
  676. bmc150_accel_get_fifo_state, NULL, 0);
  677. static IIO_DEVICE_ATTR(hwfifo_watermark, S_IRUGO,
  678. bmc150_accel_get_fifo_watermark, NULL, 0);
  679. static const struct attribute *bmc150_accel_fifo_attributes[] = {
  680. &iio_const_attr_hwfifo_watermark_min.dev_attr.attr,
  681. &iio_const_attr_hwfifo_watermark_max.dev_attr.attr,
  682. &iio_dev_attr_hwfifo_watermark.dev_attr.attr,
  683. &iio_dev_attr_hwfifo_enabled.dev_attr.attr,
  684. NULL,
  685. };
  686. static int bmc150_accel_set_watermark(struct iio_dev *indio_dev, unsigned val)
  687. {
  688. struct bmc150_accel_data *data = iio_priv(indio_dev);
  689. if (val > BMC150_ACCEL_FIFO_LENGTH)
  690. val = BMC150_ACCEL_FIFO_LENGTH;
  691. mutex_lock(&data->mutex);
  692. data->watermark = val;
  693. mutex_unlock(&data->mutex);
  694. return 0;
  695. }
  696. /*
  697. * We must read at least one full frame in one burst, otherwise the rest of the
  698. * frame data is discarded.
  699. */
  700. static int bmc150_accel_fifo_transfer(struct bmc150_accel_data *data,
  701. char *buffer, int samples)
  702. {
  703. int sample_length = 3 * 2;
  704. int ret;
  705. int total_length = samples * sample_length;
  706. int i;
  707. size_t step = regmap_get_raw_read_max(data->regmap);
  708. if (!step || step > total_length)
  709. step = total_length;
  710. else if (step < total_length)
  711. step = sample_length;
  712. /*
  713. * Seems we have a bus with size limitation so we have to execute
  714. * multiple reads
  715. */
  716. for (i = 0; i < total_length; i += step) {
  717. ret = regmap_raw_read(data->regmap, BMC150_ACCEL_REG_FIFO_DATA,
  718. &buffer[i], step);
  719. if (ret)
  720. break;
  721. }
  722. if (ret)
  723. dev_err(data->dev, "Error transferring data from fifo in single steps of %zu\n",
  724. step);
  725. return ret;
  726. }
  727. static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
  728. unsigned samples, bool irq)
  729. {
  730. struct bmc150_accel_data *data = iio_priv(indio_dev);
  731. int ret, i;
  732. u8 count;
  733. u16 buffer[BMC150_ACCEL_FIFO_LENGTH * 3];
  734. int64_t tstamp;
  735. uint64_t sample_period;
  736. unsigned int val;
  737. ret = regmap_read(data->regmap, BMC150_ACCEL_REG_FIFO_STATUS, &val);
  738. if (ret < 0) {
  739. dev_err(data->dev, "Error reading reg_fifo_status\n");
  740. return ret;
  741. }
  742. count = val & 0x7F;
  743. if (!count)
  744. return 0;
  745. /*
  746. * If we getting called from IRQ handler we know the stored timestamp is
  747. * fairly accurate for the last stored sample. Otherwise, if we are
  748. * called as a result of a read operation from userspace and hence
  749. * before the watermark interrupt was triggered, take a timestamp
  750. * now. We can fall anywhere in between two samples so the error in this
  751. * case is at most one sample period.
  752. */
  753. if (!irq) {
  754. data->old_timestamp = data->timestamp;
  755. data->timestamp = iio_get_time_ns();
  756. }
  757. /*
  758. * Approximate timestamps for each of the sample based on the sampling
  759. * frequency, timestamp for last sample and number of samples.
  760. *
  761. * Note that we can't use the current bandwidth settings to compute the
  762. * sample period because the sample rate varies with the device
  763. * (e.g. between 31.70ms to 32.20ms for a bandwidth of 15.63HZ). That
  764. * small variation adds when we store a large number of samples and
  765. * creates significant jitter between the last and first samples in
  766. * different batches (e.g. 32ms vs 21ms).
  767. *
  768. * To avoid this issue we compute the actual sample period ourselves
  769. * based on the timestamp delta between the last two flush operations.
  770. */
  771. sample_period = (data->timestamp - data->old_timestamp);
  772. do_div(sample_period, count);
  773. tstamp = data->timestamp - (count - 1) * sample_period;
  774. if (samples && count > samples)
  775. count = samples;
  776. ret = bmc150_accel_fifo_transfer(data, (u8 *)buffer, count);
  777. if (ret)
  778. return ret;
  779. /*
  780. * Ideally we want the IIO core to handle the demux when running in fifo
  781. * mode but not when running in triggered buffer mode. Unfortunately
  782. * this does not seem to be possible, so stick with driver demux for
  783. * now.
  784. */
  785. for (i = 0; i < count; i++) {
  786. u16 sample[8];
  787. int j, bit;
  788. j = 0;
  789. for_each_set_bit(bit, indio_dev->active_scan_mask,
  790. indio_dev->masklength)
  791. memcpy(&sample[j++], &buffer[i * 3 + bit], 2);
  792. iio_push_to_buffers_with_timestamp(indio_dev, sample, tstamp);
  793. tstamp += sample_period;
  794. }
  795. return count;
  796. }
  797. static int bmc150_accel_fifo_flush(struct iio_dev *indio_dev, unsigned samples)
  798. {
  799. struct bmc150_accel_data *data = iio_priv(indio_dev);
  800. int ret;
  801. mutex_lock(&data->mutex);
  802. ret = __bmc150_accel_fifo_flush(indio_dev, samples, false);
  803. mutex_unlock(&data->mutex);
  804. return ret;
  805. }
  806. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
  807. "15.620000 31.260000 62.50000 125 250 500 1000 2000");
  808. static struct attribute *bmc150_accel_attributes[] = {
  809. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  810. NULL,
  811. };
  812. static const struct attribute_group bmc150_accel_attrs_group = {
  813. .attrs = bmc150_accel_attributes,
  814. };
  815. static const struct iio_event_spec bmc150_accel_event = {
  816. .type = IIO_EV_TYPE_ROC,
  817. .dir = IIO_EV_DIR_EITHER,
  818. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  819. BIT(IIO_EV_INFO_ENABLE) |
  820. BIT(IIO_EV_INFO_PERIOD)
  821. };
  822. #define BMC150_ACCEL_CHANNEL(_axis, bits) { \
  823. .type = IIO_ACCEL, \
  824. .modified = 1, \
  825. .channel2 = IIO_MOD_##_axis, \
  826. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  827. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  828. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  829. .scan_index = AXIS_##_axis, \
  830. .scan_type = { \
  831. .sign = 's', \
  832. .realbits = (bits), \
  833. .storagebits = 16, \
  834. .shift = 16 - (bits), \
  835. .endianness = IIO_LE, \
  836. }, \
  837. .event_spec = &bmc150_accel_event, \
  838. .num_event_specs = 1 \
  839. }
  840. #define BMC150_ACCEL_CHANNELS(bits) { \
  841. { \
  842. .type = IIO_TEMP, \
  843. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  844. BIT(IIO_CHAN_INFO_SCALE) | \
  845. BIT(IIO_CHAN_INFO_OFFSET), \
  846. .scan_index = -1, \
  847. }, \
  848. BMC150_ACCEL_CHANNEL(X, bits), \
  849. BMC150_ACCEL_CHANNEL(Y, bits), \
  850. BMC150_ACCEL_CHANNEL(Z, bits), \
  851. IIO_CHAN_SOFT_TIMESTAMP(3), \
  852. }
  853. static const struct iio_chan_spec bma222e_accel_channels[] =
  854. BMC150_ACCEL_CHANNELS(8);
  855. static const struct iio_chan_spec bma250e_accel_channels[] =
  856. BMC150_ACCEL_CHANNELS(10);
  857. static const struct iio_chan_spec bmc150_accel_channels[] =
  858. BMC150_ACCEL_CHANNELS(12);
  859. static const struct iio_chan_spec bma280_accel_channels[] =
  860. BMC150_ACCEL_CHANNELS(14);
  861. static const struct bmc150_accel_chip_info bmc150_accel_chip_info_tbl[] = {
  862. [bmc150] = {
  863. .name = "BMC150A",
  864. .chip_id = 0xFA,
  865. .channels = bmc150_accel_channels,
  866. .num_channels = ARRAY_SIZE(bmc150_accel_channels),
  867. .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G},
  868. {19122, BMC150_ACCEL_DEF_RANGE_4G},
  869. {38344, BMC150_ACCEL_DEF_RANGE_8G},
  870. {76590, BMC150_ACCEL_DEF_RANGE_16G} },
  871. },
  872. [bmi055] = {
  873. .name = "BMI055A",
  874. .chip_id = 0xFA,
  875. .channels = bmc150_accel_channels,
  876. .num_channels = ARRAY_SIZE(bmc150_accel_channels),
  877. .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G},
  878. {19122, BMC150_ACCEL_DEF_RANGE_4G},
  879. {38344, BMC150_ACCEL_DEF_RANGE_8G},
  880. {76590, BMC150_ACCEL_DEF_RANGE_16G} },
  881. },
  882. [bma255] = {
  883. .name = "BMA0255",
  884. .chip_id = 0xFA,
  885. .channels = bmc150_accel_channels,
  886. .num_channels = ARRAY_SIZE(bmc150_accel_channels),
  887. .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G},
  888. {19122, BMC150_ACCEL_DEF_RANGE_4G},
  889. {38344, BMC150_ACCEL_DEF_RANGE_8G},
  890. {76590, BMC150_ACCEL_DEF_RANGE_16G} },
  891. },
  892. [bma250e] = {
  893. .name = "BMA250E",
  894. .chip_id = 0xF9,
  895. .channels = bma250e_accel_channels,
  896. .num_channels = ARRAY_SIZE(bma250e_accel_channels),
  897. .scale_table = { {38344, BMC150_ACCEL_DEF_RANGE_2G},
  898. {76590, BMC150_ACCEL_DEF_RANGE_4G},
  899. {153277, BMC150_ACCEL_DEF_RANGE_8G},
  900. {306457, BMC150_ACCEL_DEF_RANGE_16G} },
  901. },
  902. [bma222e] = {
  903. .name = "BMA222E",
  904. .chip_id = 0xF8,
  905. .channels = bma222e_accel_channels,
  906. .num_channels = ARRAY_SIZE(bma222e_accel_channels),
  907. .scale_table = { {153277, BMC150_ACCEL_DEF_RANGE_2G},
  908. {306457, BMC150_ACCEL_DEF_RANGE_4G},
  909. {612915, BMC150_ACCEL_DEF_RANGE_8G},
  910. {1225831, BMC150_ACCEL_DEF_RANGE_16G} },
  911. },
  912. [bma280] = {
  913. .name = "BMA0280",
  914. .chip_id = 0xFB,
  915. .channels = bma280_accel_channels,
  916. .num_channels = ARRAY_SIZE(bma280_accel_channels),
  917. .scale_table = { {2392, BMC150_ACCEL_DEF_RANGE_2G},
  918. {4785, BMC150_ACCEL_DEF_RANGE_4G},
  919. {9581, BMC150_ACCEL_DEF_RANGE_8G},
  920. {19152, BMC150_ACCEL_DEF_RANGE_16G} },
  921. },
  922. };
  923. static const struct iio_info bmc150_accel_info = {
  924. .attrs = &bmc150_accel_attrs_group,
  925. .read_raw = bmc150_accel_read_raw,
  926. .write_raw = bmc150_accel_write_raw,
  927. .read_event_value = bmc150_accel_read_event,
  928. .write_event_value = bmc150_accel_write_event,
  929. .write_event_config = bmc150_accel_write_event_config,
  930. .read_event_config = bmc150_accel_read_event_config,
  931. .driver_module = THIS_MODULE,
  932. };
  933. static const struct iio_info bmc150_accel_info_fifo = {
  934. .attrs = &bmc150_accel_attrs_group,
  935. .read_raw = bmc150_accel_read_raw,
  936. .write_raw = bmc150_accel_write_raw,
  937. .read_event_value = bmc150_accel_read_event,
  938. .write_event_value = bmc150_accel_write_event,
  939. .write_event_config = bmc150_accel_write_event_config,
  940. .read_event_config = bmc150_accel_read_event_config,
  941. .validate_trigger = bmc150_accel_validate_trigger,
  942. .hwfifo_set_watermark = bmc150_accel_set_watermark,
  943. .hwfifo_flush_to_buffer = bmc150_accel_fifo_flush,
  944. .driver_module = THIS_MODULE,
  945. };
  946. static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p)
  947. {
  948. struct iio_poll_func *pf = p;
  949. struct iio_dev *indio_dev = pf->indio_dev;
  950. struct bmc150_accel_data *data = iio_priv(indio_dev);
  951. int bit, ret, i = 0;
  952. unsigned int raw_val;
  953. mutex_lock(&data->mutex);
  954. for_each_set_bit(bit, indio_dev->active_scan_mask,
  955. indio_dev->masklength) {
  956. ret = regmap_bulk_read(data->regmap,
  957. BMC150_ACCEL_AXIS_TO_REG(bit), &raw_val,
  958. 2);
  959. if (ret < 0) {
  960. mutex_unlock(&data->mutex);
  961. goto err_read;
  962. }
  963. data->buffer[i++] = raw_val;
  964. }
  965. mutex_unlock(&data->mutex);
  966. iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
  967. pf->timestamp);
  968. err_read:
  969. iio_trigger_notify_done(indio_dev->trig);
  970. return IRQ_HANDLED;
  971. }
  972. static int bmc150_accel_trig_try_reen(struct iio_trigger *trig)
  973. {
  974. struct bmc150_accel_trigger *t = iio_trigger_get_drvdata(trig);
  975. struct bmc150_accel_data *data = t->data;
  976. int ret;
  977. /* new data interrupts don't need ack */
  978. if (t == &t->data->triggers[BMC150_ACCEL_TRIGGER_DATA_READY])
  979. return 0;
  980. mutex_lock(&data->mutex);
  981. /* clear any latched interrupt */
  982. ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
  983. BMC150_ACCEL_INT_MODE_LATCH_INT |
  984. BMC150_ACCEL_INT_MODE_LATCH_RESET);
  985. mutex_unlock(&data->mutex);
  986. if (ret < 0) {
  987. dev_err(data->dev,
  988. "Error writing reg_int_rst_latch\n");
  989. return ret;
  990. }
  991. return 0;
  992. }
  993. static int bmc150_accel_trigger_set_state(struct iio_trigger *trig,
  994. bool state)
  995. {
  996. struct bmc150_accel_trigger *t = iio_trigger_get_drvdata(trig);
  997. struct bmc150_accel_data *data = t->data;
  998. int ret;
  999. mutex_lock(&data->mutex);
  1000. if (t->enabled == state) {
  1001. mutex_unlock(&data->mutex);
  1002. return 0;
  1003. }
  1004. if (t->setup) {
  1005. ret = t->setup(t, state);
  1006. if (ret < 0) {
  1007. mutex_unlock(&data->mutex);
  1008. return ret;
  1009. }
  1010. }
  1011. ret = bmc150_accel_set_interrupt(data, t->intr, state);
  1012. if (ret < 0) {
  1013. mutex_unlock(&data->mutex);
  1014. return ret;
  1015. }
  1016. t->enabled = state;
  1017. mutex_unlock(&data->mutex);
  1018. return ret;
  1019. }
  1020. static const struct iio_trigger_ops bmc150_accel_trigger_ops = {
  1021. .set_trigger_state = bmc150_accel_trigger_set_state,
  1022. .try_reenable = bmc150_accel_trig_try_reen,
  1023. .owner = THIS_MODULE,
  1024. };
  1025. static int bmc150_accel_handle_roc_event(struct iio_dev *indio_dev)
  1026. {
  1027. struct bmc150_accel_data *data = iio_priv(indio_dev);
  1028. int dir;
  1029. int ret;
  1030. unsigned int val;
  1031. ret = regmap_read(data->regmap, BMC150_ACCEL_REG_INT_STATUS_2, &val);
  1032. if (ret < 0) {
  1033. dev_err(data->dev, "Error reading reg_int_status_2\n");
  1034. return ret;
  1035. }
  1036. if (val & BMC150_ACCEL_ANY_MOTION_BIT_SIGN)
  1037. dir = IIO_EV_DIR_FALLING;
  1038. else
  1039. dir = IIO_EV_DIR_RISING;
  1040. if (val & BMC150_ACCEL_ANY_MOTION_BIT_X)
  1041. iio_push_event(indio_dev,
  1042. IIO_MOD_EVENT_CODE(IIO_ACCEL,
  1043. 0,
  1044. IIO_MOD_X,
  1045. IIO_EV_TYPE_ROC,
  1046. dir),
  1047. data->timestamp);
  1048. if (val & BMC150_ACCEL_ANY_MOTION_BIT_Y)
  1049. iio_push_event(indio_dev,
  1050. IIO_MOD_EVENT_CODE(IIO_ACCEL,
  1051. 0,
  1052. IIO_MOD_Y,
  1053. IIO_EV_TYPE_ROC,
  1054. dir),
  1055. data->timestamp);
  1056. if (val & BMC150_ACCEL_ANY_MOTION_BIT_Z)
  1057. iio_push_event(indio_dev,
  1058. IIO_MOD_EVENT_CODE(IIO_ACCEL,
  1059. 0,
  1060. IIO_MOD_Z,
  1061. IIO_EV_TYPE_ROC,
  1062. dir),
  1063. data->timestamp);
  1064. return ret;
  1065. }
  1066. static irqreturn_t bmc150_accel_irq_thread_handler(int irq, void *private)
  1067. {
  1068. struct iio_dev *indio_dev = private;
  1069. struct bmc150_accel_data *data = iio_priv(indio_dev);
  1070. bool ack = false;
  1071. int ret;
  1072. mutex_lock(&data->mutex);
  1073. if (data->fifo_mode) {
  1074. ret = __bmc150_accel_fifo_flush(indio_dev,
  1075. BMC150_ACCEL_FIFO_LENGTH, true);
  1076. if (ret > 0)
  1077. ack = true;
  1078. }
  1079. if (data->ev_enable_state) {
  1080. ret = bmc150_accel_handle_roc_event(indio_dev);
  1081. if (ret > 0)
  1082. ack = true;
  1083. }
  1084. if (ack) {
  1085. ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
  1086. BMC150_ACCEL_INT_MODE_LATCH_INT |
  1087. BMC150_ACCEL_INT_MODE_LATCH_RESET);
  1088. if (ret)
  1089. dev_err(data->dev, "Error writing reg_int_rst_latch\n");
  1090. ret = IRQ_HANDLED;
  1091. } else {
  1092. ret = IRQ_NONE;
  1093. }
  1094. mutex_unlock(&data->mutex);
  1095. return ret;
  1096. }
  1097. static irqreturn_t bmc150_accel_irq_handler(int irq, void *private)
  1098. {
  1099. struct iio_dev *indio_dev = private;
  1100. struct bmc150_accel_data *data = iio_priv(indio_dev);
  1101. bool ack = false;
  1102. int i;
  1103. data->old_timestamp = data->timestamp;
  1104. data->timestamp = iio_get_time_ns();
  1105. for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) {
  1106. if (data->triggers[i].enabled) {
  1107. iio_trigger_poll(data->triggers[i].indio_trig);
  1108. ack = true;
  1109. break;
  1110. }
  1111. }
  1112. if (data->ev_enable_state || data->fifo_mode)
  1113. return IRQ_WAKE_THREAD;
  1114. if (ack)
  1115. return IRQ_HANDLED;
  1116. return IRQ_NONE;
  1117. }
  1118. static const struct {
  1119. int intr;
  1120. const char *name;
  1121. int (*setup)(struct bmc150_accel_trigger *t, bool state);
  1122. } bmc150_accel_triggers[BMC150_ACCEL_TRIGGERS] = {
  1123. {
  1124. .intr = 0,
  1125. .name = "%s-dev%d",
  1126. },
  1127. {
  1128. .intr = 1,
  1129. .name = "%s-any-motion-dev%d",
  1130. .setup = bmc150_accel_any_motion_setup,
  1131. },
  1132. };
  1133. static void bmc150_accel_unregister_triggers(struct bmc150_accel_data *data,
  1134. int from)
  1135. {
  1136. int i;
  1137. for (i = from; i >= 0; i--) {
  1138. if (data->triggers[i].indio_trig) {
  1139. iio_trigger_unregister(data->triggers[i].indio_trig);
  1140. data->triggers[i].indio_trig = NULL;
  1141. }
  1142. }
  1143. }
  1144. static int bmc150_accel_triggers_setup(struct iio_dev *indio_dev,
  1145. struct bmc150_accel_data *data)
  1146. {
  1147. int i, ret;
  1148. for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) {
  1149. struct bmc150_accel_trigger *t = &data->triggers[i];
  1150. t->indio_trig = devm_iio_trigger_alloc(data->dev,
  1151. bmc150_accel_triggers[i].name,
  1152. indio_dev->name,
  1153. indio_dev->id);
  1154. if (!t->indio_trig) {
  1155. ret = -ENOMEM;
  1156. break;
  1157. }
  1158. t->indio_trig->dev.parent = data->dev;
  1159. t->indio_trig->ops = &bmc150_accel_trigger_ops;
  1160. t->intr = bmc150_accel_triggers[i].intr;
  1161. t->data = data;
  1162. t->setup = bmc150_accel_triggers[i].setup;
  1163. iio_trigger_set_drvdata(t->indio_trig, t);
  1164. ret = iio_trigger_register(t->indio_trig);
  1165. if (ret)
  1166. break;
  1167. }
  1168. if (ret)
  1169. bmc150_accel_unregister_triggers(data, i - 1);
  1170. return ret;
  1171. }
  1172. #define BMC150_ACCEL_FIFO_MODE_STREAM 0x80
  1173. #define BMC150_ACCEL_FIFO_MODE_FIFO 0x40
  1174. #define BMC150_ACCEL_FIFO_MODE_BYPASS 0x00
  1175. static int bmc150_accel_fifo_set_mode(struct bmc150_accel_data *data)
  1176. {
  1177. u8 reg = BMC150_ACCEL_REG_FIFO_CONFIG1;
  1178. int ret;
  1179. ret = regmap_write(data->regmap, reg, data->fifo_mode);
  1180. if (ret < 0) {
  1181. dev_err(data->dev, "Error writing reg_fifo_config1\n");
  1182. return ret;
  1183. }
  1184. if (!data->fifo_mode)
  1185. return 0;
  1186. ret = regmap_write(data->regmap, BMC150_ACCEL_REG_FIFO_CONFIG0,
  1187. data->watermark);
  1188. if (ret < 0)
  1189. dev_err(data->dev, "Error writing reg_fifo_config0\n");
  1190. return ret;
  1191. }
  1192. static int bmc150_accel_buffer_preenable(struct iio_dev *indio_dev)
  1193. {
  1194. struct bmc150_accel_data *data = iio_priv(indio_dev);
  1195. return bmc150_accel_set_power_state(data, true);
  1196. }
  1197. static int bmc150_accel_buffer_postenable(struct iio_dev *indio_dev)
  1198. {
  1199. struct bmc150_accel_data *data = iio_priv(indio_dev);
  1200. int ret = 0;
  1201. if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED)
  1202. return iio_triggered_buffer_postenable(indio_dev);
  1203. mutex_lock(&data->mutex);
  1204. if (!data->watermark)
  1205. goto out;
  1206. ret = bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_WATERMARK,
  1207. true);
  1208. if (ret)
  1209. goto out;
  1210. data->fifo_mode = BMC150_ACCEL_FIFO_MODE_FIFO;
  1211. ret = bmc150_accel_fifo_set_mode(data);
  1212. if (ret) {
  1213. data->fifo_mode = 0;
  1214. bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_WATERMARK,
  1215. false);
  1216. }
  1217. out:
  1218. mutex_unlock(&data->mutex);
  1219. return ret;
  1220. }
  1221. static int bmc150_accel_buffer_predisable(struct iio_dev *indio_dev)
  1222. {
  1223. struct bmc150_accel_data *data = iio_priv(indio_dev);
  1224. if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED)
  1225. return iio_triggered_buffer_predisable(indio_dev);
  1226. mutex_lock(&data->mutex);
  1227. if (!data->fifo_mode)
  1228. goto out;
  1229. bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_WATERMARK, false);
  1230. __bmc150_accel_fifo_flush(indio_dev, BMC150_ACCEL_FIFO_LENGTH, false);
  1231. data->fifo_mode = 0;
  1232. bmc150_accel_fifo_set_mode(data);
  1233. out:
  1234. mutex_unlock(&data->mutex);
  1235. return 0;
  1236. }
  1237. static int bmc150_accel_buffer_postdisable(struct iio_dev *indio_dev)
  1238. {
  1239. struct bmc150_accel_data *data = iio_priv(indio_dev);
  1240. return bmc150_accel_set_power_state(data, false);
  1241. }
  1242. static const struct iio_buffer_setup_ops bmc150_accel_buffer_ops = {
  1243. .preenable = bmc150_accel_buffer_preenable,
  1244. .postenable = bmc150_accel_buffer_postenable,
  1245. .predisable = bmc150_accel_buffer_predisable,
  1246. .postdisable = bmc150_accel_buffer_postdisable,
  1247. };
  1248. static int bmc150_accel_chip_init(struct bmc150_accel_data *data)
  1249. {
  1250. int ret, i;
  1251. unsigned int val;
  1252. /*
  1253. * Reset chip to get it in a known good state. A delay of 1.8ms after
  1254. * reset is required according to the data sheets of supported chips.
  1255. */
  1256. regmap_write(data->regmap, BMC150_ACCEL_REG_RESET,
  1257. BMC150_ACCEL_RESET_VAL);
  1258. usleep_range(1800, 2500);
  1259. ret = regmap_read(data->regmap, BMC150_ACCEL_REG_CHIP_ID, &val);
  1260. if (ret < 0) {
  1261. dev_err(data->dev,
  1262. "Error: Reading chip id\n");
  1263. return ret;
  1264. }
  1265. dev_dbg(data->dev, "Chip Id %x\n", val);
  1266. for (i = 0; i < ARRAY_SIZE(bmc150_accel_chip_info_tbl); i++) {
  1267. if (bmc150_accel_chip_info_tbl[i].chip_id == val) {
  1268. data->chip_info = &bmc150_accel_chip_info_tbl[i];
  1269. break;
  1270. }
  1271. }
  1272. if (!data->chip_info) {
  1273. dev_err(data->dev, "Invalid chip %x\n", val);
  1274. return -ENODEV;
  1275. }
  1276. ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
  1277. if (ret < 0)
  1278. return ret;
  1279. /* Set Bandwidth */
  1280. ret = bmc150_accel_set_bw(data, BMC150_ACCEL_DEF_BW, 0);
  1281. if (ret < 0)
  1282. return ret;
  1283. /* Set Default Range */
  1284. ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_RANGE,
  1285. BMC150_ACCEL_DEF_RANGE_4G);
  1286. if (ret < 0) {
  1287. dev_err(data->dev,
  1288. "Error writing reg_pmu_range\n");
  1289. return ret;
  1290. }
  1291. data->range = BMC150_ACCEL_DEF_RANGE_4G;
  1292. /* Set default slope duration and thresholds */
  1293. data->slope_thres = BMC150_ACCEL_DEF_SLOPE_THRESHOLD;
  1294. data->slope_dur = BMC150_ACCEL_DEF_SLOPE_DURATION;
  1295. ret = bmc150_accel_update_slope(data);
  1296. if (ret < 0)
  1297. return ret;
  1298. /* Set default as latched interrupts */
  1299. ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
  1300. BMC150_ACCEL_INT_MODE_LATCH_INT |
  1301. BMC150_ACCEL_INT_MODE_LATCH_RESET);
  1302. if (ret < 0) {
  1303. dev_err(data->dev,
  1304. "Error writing reg_int_rst_latch\n");
  1305. return ret;
  1306. }
  1307. return 0;
  1308. }
  1309. int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq,
  1310. const char *name, bool block_supported)
  1311. {
  1312. struct bmc150_accel_data *data;
  1313. struct iio_dev *indio_dev;
  1314. int ret;
  1315. indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
  1316. if (!indio_dev)
  1317. return -ENOMEM;
  1318. data = iio_priv(indio_dev);
  1319. dev_set_drvdata(dev, indio_dev);
  1320. data->dev = dev;
  1321. data->irq = irq;
  1322. data->regmap = regmap;
  1323. ret = bmc150_accel_chip_init(data);
  1324. if (ret < 0)
  1325. return ret;
  1326. mutex_init(&data->mutex);
  1327. indio_dev->dev.parent = dev;
  1328. indio_dev->channels = data->chip_info->channels;
  1329. indio_dev->num_channels = data->chip_info->num_channels;
  1330. indio_dev->name = name ? name : data->chip_info->name;
  1331. indio_dev->modes = INDIO_DIRECT_MODE;
  1332. indio_dev->info = &bmc150_accel_info;
  1333. ret = iio_triggered_buffer_setup(indio_dev,
  1334. &iio_pollfunc_store_time,
  1335. bmc150_accel_trigger_handler,
  1336. &bmc150_accel_buffer_ops);
  1337. if (ret < 0) {
  1338. dev_err(data->dev, "Failed: iio triggered buffer setup\n");
  1339. return ret;
  1340. }
  1341. if (data->irq > 0) {
  1342. ret = devm_request_threaded_irq(
  1343. data->dev, data->irq,
  1344. bmc150_accel_irq_handler,
  1345. bmc150_accel_irq_thread_handler,
  1346. IRQF_TRIGGER_RISING,
  1347. BMC150_ACCEL_IRQ_NAME,
  1348. indio_dev);
  1349. if (ret)
  1350. goto err_buffer_cleanup;
  1351. /*
  1352. * Set latched mode interrupt. While certain interrupts are
  1353. * non-latched regardless of this settings (e.g. new data) we
  1354. * want to use latch mode when we can to prevent interrupt
  1355. * flooding.
  1356. */
  1357. ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
  1358. BMC150_ACCEL_INT_MODE_LATCH_RESET);
  1359. if (ret < 0) {
  1360. dev_err(data->dev, "Error writing reg_int_rst_latch\n");
  1361. goto err_buffer_cleanup;
  1362. }
  1363. bmc150_accel_interrupts_setup(indio_dev, data);
  1364. ret = bmc150_accel_triggers_setup(indio_dev, data);
  1365. if (ret)
  1366. goto err_buffer_cleanup;
  1367. if (block_supported) {
  1368. indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
  1369. indio_dev->info = &bmc150_accel_info_fifo;
  1370. indio_dev->buffer->attrs = bmc150_accel_fifo_attributes;
  1371. }
  1372. }
  1373. ret = iio_device_register(indio_dev);
  1374. if (ret < 0) {
  1375. dev_err(dev, "Unable to register iio device\n");
  1376. goto err_trigger_unregister;
  1377. }
  1378. ret = pm_runtime_set_active(dev);
  1379. if (ret)
  1380. goto err_iio_unregister;
  1381. pm_runtime_enable(dev);
  1382. pm_runtime_set_autosuspend_delay(dev, BMC150_AUTO_SUSPEND_DELAY_MS);
  1383. pm_runtime_use_autosuspend(dev);
  1384. return 0;
  1385. err_iio_unregister:
  1386. iio_device_unregister(indio_dev);
  1387. err_trigger_unregister:
  1388. bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1);
  1389. err_buffer_cleanup:
  1390. iio_triggered_buffer_cleanup(indio_dev);
  1391. return ret;
  1392. }
  1393. EXPORT_SYMBOL_GPL(bmc150_accel_core_probe);
  1394. int bmc150_accel_core_remove(struct device *dev)
  1395. {
  1396. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1397. struct bmc150_accel_data *data = iio_priv(indio_dev);
  1398. pm_runtime_disable(data->dev);
  1399. pm_runtime_set_suspended(data->dev);
  1400. pm_runtime_put_noidle(data->dev);
  1401. iio_device_unregister(indio_dev);
  1402. bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1);
  1403. iio_triggered_buffer_cleanup(indio_dev);
  1404. mutex_lock(&data->mutex);
  1405. bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND, 0);
  1406. mutex_unlock(&data->mutex);
  1407. return 0;
  1408. }
  1409. EXPORT_SYMBOL_GPL(bmc150_accel_core_remove);
  1410. #ifdef CONFIG_PM_SLEEP
  1411. static int bmc150_accel_suspend(struct device *dev)
  1412. {
  1413. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1414. struct bmc150_accel_data *data = iio_priv(indio_dev);
  1415. mutex_lock(&data->mutex);
  1416. bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0);
  1417. mutex_unlock(&data->mutex);
  1418. return 0;
  1419. }
  1420. static int bmc150_accel_resume(struct device *dev)
  1421. {
  1422. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1423. struct bmc150_accel_data *data = iio_priv(indio_dev);
  1424. mutex_lock(&data->mutex);
  1425. bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
  1426. bmc150_accel_fifo_set_mode(data);
  1427. mutex_unlock(&data->mutex);
  1428. return 0;
  1429. }
  1430. #endif
  1431. #ifdef CONFIG_PM
  1432. static int bmc150_accel_runtime_suspend(struct device *dev)
  1433. {
  1434. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1435. struct bmc150_accel_data *data = iio_priv(indio_dev);
  1436. int ret;
  1437. dev_dbg(data->dev, __func__);
  1438. ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0);
  1439. if (ret < 0)
  1440. return -EAGAIN;
  1441. return 0;
  1442. }
  1443. static int bmc150_accel_runtime_resume(struct device *dev)
  1444. {
  1445. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1446. struct bmc150_accel_data *data = iio_priv(indio_dev);
  1447. int ret;
  1448. int sleep_val;
  1449. dev_dbg(data->dev, __func__);
  1450. ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
  1451. if (ret < 0)
  1452. return ret;
  1453. ret = bmc150_accel_fifo_set_mode(data);
  1454. if (ret < 0)
  1455. return ret;
  1456. sleep_val = bmc150_accel_get_startup_times(data);
  1457. if (sleep_val < 20)
  1458. usleep_range(sleep_val * 1000, 20000);
  1459. else
  1460. msleep_interruptible(sleep_val);
  1461. return 0;
  1462. }
  1463. #endif
  1464. const struct dev_pm_ops bmc150_accel_pm_ops = {
  1465. SET_SYSTEM_SLEEP_PM_OPS(bmc150_accel_suspend, bmc150_accel_resume)
  1466. SET_RUNTIME_PM_OPS(bmc150_accel_runtime_suspend,
  1467. bmc150_accel_runtime_resume, NULL)
  1468. };
  1469. EXPORT_SYMBOL_GPL(bmc150_accel_pm_ops);
  1470. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  1471. MODULE_LICENSE("GPL v2");
  1472. MODULE_DESCRIPTION("BMC150 accelerometer driver");