bmc150-accel.c 36 KB

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