stk8ba50.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /**
  2. * Sensortek STK8BA50 3-Axis Accelerometer
  3. *
  4. * Copyright (c) 2015, Intel Corporation.
  5. *
  6. * This file is subject to the terms and conditions of version 2 of
  7. * the GNU General Public License. See the file COPYING in the main
  8. * directory of this archive for more details.
  9. *
  10. * STK8BA50 7-bit I2C address: 0x18.
  11. */
  12. #include <linux/acpi.h>
  13. #include <linux/i2c.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/iio/buffer.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/iio/sysfs.h>
  20. #include <linux/iio/trigger.h>
  21. #include <linux/iio/triggered_buffer.h>
  22. #include <linux/iio/trigger_consumer.h>
  23. #define STK8BA50_REG_XOUT 0x02
  24. #define STK8BA50_REG_YOUT 0x04
  25. #define STK8BA50_REG_ZOUT 0x06
  26. #define STK8BA50_REG_RANGE 0x0F
  27. #define STK8BA50_REG_BWSEL 0x10
  28. #define STK8BA50_REG_POWMODE 0x11
  29. #define STK8BA50_REG_SWRST 0x14
  30. #define STK8BA50_REG_INTEN2 0x17
  31. #define STK8BA50_REG_INTMAP2 0x1A
  32. #define STK8BA50_MODE_NORMAL 0
  33. #define STK8BA50_MODE_SUSPEND 1
  34. #define STK8BA50_MODE_POWERBIT BIT(7)
  35. #define STK8BA50_DATA_SHIFT 6
  36. #define STK8BA50_RESET_CMD 0xB6
  37. #define STK8BA50_SR_1792HZ_IDX 7
  38. #define STK8BA50_DREADY_INT_MASK 0x10
  39. #define STK8BA50_DREADY_INT_MAP 0x81
  40. #define STK8BA50_ALL_CHANNEL_MASK 7
  41. #define STK8BA50_ALL_CHANNEL_SIZE 6
  42. #define STK8BA50_DRIVER_NAME "stk8ba50"
  43. #define STK8BA50_IRQ_NAME "stk8ba50_event"
  44. #define STK8BA50_SCALE_AVAIL "0.0384 0.0767 0.1534 0.3069"
  45. /*
  46. * The accelerometer has four measurement ranges:
  47. * +/-2g; +/-4g; +/-8g; +/-16g
  48. *
  49. * Acceleration values are 10-bit, 2's complement.
  50. * Scales are calculated as following:
  51. *
  52. * scale1 = (2 + 2) * 9.81 / (2^10 - 1) = 0.0384
  53. * scale2 = (4 + 4) * 9.81 / (2^10 - 1) = 0.0767
  54. * etc.
  55. *
  56. * Scales are stored in this format:
  57. * { <register value>, <scale value> }
  58. *
  59. * Locally, the range is stored as a table index.
  60. */
  61. static const struct {
  62. u8 reg_val;
  63. u32 scale_val;
  64. } stk8ba50_scale_table[] = {
  65. {3, 38400}, {5, 76700}, {8, 153400}, {12, 306900}
  66. };
  67. /* Sample rates are stored as { <register value>, <Hz value> } */
  68. static const struct {
  69. u8 reg_val;
  70. u16 samp_freq;
  71. } stk8ba50_samp_freq_table[] = {
  72. {0x08, 14}, {0x09, 25}, {0x0A, 56}, {0x0B, 112},
  73. {0x0C, 224}, {0x0D, 448}, {0x0E, 896}, {0x0F, 1792}
  74. };
  75. /* Used to map scan mask bits to their corresponding channel register. */
  76. static const int stk8ba50_channel_table[] = {
  77. STK8BA50_REG_XOUT,
  78. STK8BA50_REG_YOUT,
  79. STK8BA50_REG_ZOUT
  80. };
  81. struct stk8ba50_data {
  82. struct i2c_client *client;
  83. struct mutex lock;
  84. int range;
  85. u8 sample_rate_idx;
  86. struct iio_trigger *dready_trig;
  87. bool dready_trigger_on;
  88. /* Ensure timestamp is naturally aligned */
  89. struct {
  90. s16 chans[3];
  91. s64 timetamp __aligned(8);
  92. } scan;
  93. };
  94. #define STK8BA50_ACCEL_CHANNEL(index, reg, axis) { \
  95. .type = IIO_ACCEL, \
  96. .address = reg, \
  97. .modified = 1, \
  98. .channel2 = IIO_MOD_##axis, \
  99. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  100. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  101. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  102. .scan_index = index, \
  103. .scan_type = { \
  104. .sign = 's', \
  105. .realbits = 10, \
  106. .storagebits = 16, \
  107. .shift = STK8BA50_DATA_SHIFT, \
  108. .endianness = IIO_CPU, \
  109. }, \
  110. }
  111. static const struct iio_chan_spec stk8ba50_channels[] = {
  112. STK8BA50_ACCEL_CHANNEL(0, STK8BA50_REG_XOUT, X),
  113. STK8BA50_ACCEL_CHANNEL(1, STK8BA50_REG_YOUT, Y),
  114. STK8BA50_ACCEL_CHANNEL(2, STK8BA50_REG_ZOUT, Z),
  115. IIO_CHAN_SOFT_TIMESTAMP(3),
  116. };
  117. static IIO_CONST_ATTR(in_accel_scale_available, STK8BA50_SCALE_AVAIL);
  118. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("14 25 56 112 224 448 896 1792");
  119. static struct attribute *stk8ba50_attributes[] = {
  120. &iio_const_attr_in_accel_scale_available.dev_attr.attr,
  121. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  122. NULL,
  123. };
  124. static const struct attribute_group stk8ba50_attribute_group = {
  125. .attrs = stk8ba50_attributes
  126. };
  127. static int stk8ba50_read_accel(struct stk8ba50_data *data, u8 reg)
  128. {
  129. int ret;
  130. struct i2c_client *client = data->client;
  131. ret = i2c_smbus_read_word_data(client, reg);
  132. if (ret < 0) {
  133. dev_err(&client->dev, "register read failed\n");
  134. return ret;
  135. }
  136. return ret;
  137. }
  138. static int stk8ba50_data_rdy_trigger_set_state(struct iio_trigger *trig,
  139. bool state)
  140. {
  141. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  142. struct stk8ba50_data *data = iio_priv(indio_dev);
  143. int ret;
  144. if (state)
  145. ret = i2c_smbus_write_byte_data(data->client,
  146. STK8BA50_REG_INTEN2, STK8BA50_DREADY_INT_MASK);
  147. else
  148. ret = i2c_smbus_write_byte_data(data->client,
  149. STK8BA50_REG_INTEN2, 0x00);
  150. if (ret < 0)
  151. dev_err(&data->client->dev, "failed to set trigger state\n");
  152. else
  153. data->dready_trigger_on = state;
  154. return ret;
  155. }
  156. static const struct iio_trigger_ops stk8ba50_trigger_ops = {
  157. .set_trigger_state = stk8ba50_data_rdy_trigger_set_state,
  158. .owner = THIS_MODULE,
  159. };
  160. static int stk8ba50_set_power(struct stk8ba50_data *data, bool mode)
  161. {
  162. int ret;
  163. u8 masked_reg;
  164. struct i2c_client *client = data->client;
  165. ret = i2c_smbus_read_byte_data(client, STK8BA50_REG_POWMODE);
  166. if (ret < 0)
  167. goto exit_err;
  168. if (mode)
  169. masked_reg = ret | STK8BA50_MODE_POWERBIT;
  170. else
  171. masked_reg = ret & (~STK8BA50_MODE_POWERBIT);
  172. ret = i2c_smbus_write_byte_data(client, STK8BA50_REG_POWMODE,
  173. masked_reg);
  174. if (ret < 0)
  175. goto exit_err;
  176. return ret;
  177. exit_err:
  178. dev_err(&client->dev, "failed to change sensor mode\n");
  179. return ret;
  180. }
  181. static int stk8ba50_read_raw(struct iio_dev *indio_dev,
  182. struct iio_chan_spec const *chan,
  183. int *val, int *val2, long mask)
  184. {
  185. struct stk8ba50_data *data = iio_priv(indio_dev);
  186. int ret;
  187. switch (mask) {
  188. case IIO_CHAN_INFO_RAW:
  189. if (iio_buffer_enabled(indio_dev))
  190. return -EBUSY;
  191. mutex_lock(&data->lock);
  192. ret = stk8ba50_set_power(data, STK8BA50_MODE_NORMAL);
  193. if (ret < 0) {
  194. mutex_unlock(&data->lock);
  195. return -EINVAL;
  196. }
  197. ret = stk8ba50_read_accel(data, chan->address);
  198. if (ret < 0) {
  199. stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
  200. mutex_unlock(&data->lock);
  201. return -EINVAL;
  202. }
  203. *val = sign_extend32(ret >> STK8BA50_DATA_SHIFT, 9);
  204. stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
  205. mutex_unlock(&data->lock);
  206. return IIO_VAL_INT;
  207. case IIO_CHAN_INFO_SCALE:
  208. *val = 0;
  209. *val2 = stk8ba50_scale_table[data->range].scale_val;
  210. return IIO_VAL_INT_PLUS_MICRO;
  211. case IIO_CHAN_INFO_SAMP_FREQ:
  212. *val = stk8ba50_samp_freq_table
  213. [data->sample_rate_idx].samp_freq;
  214. *val2 = 0;
  215. return IIO_VAL_INT;
  216. }
  217. return -EINVAL;
  218. }
  219. static int stk8ba50_write_raw(struct iio_dev *indio_dev,
  220. struct iio_chan_spec const *chan,
  221. int val, int val2, long mask)
  222. {
  223. int ret;
  224. int i;
  225. int index = -1;
  226. struct stk8ba50_data *data = iio_priv(indio_dev);
  227. switch (mask) {
  228. case IIO_CHAN_INFO_SCALE:
  229. if (val != 0)
  230. return -EINVAL;
  231. for (i = 0; i < ARRAY_SIZE(stk8ba50_scale_table); i++)
  232. if (val2 == stk8ba50_scale_table[i].scale_val) {
  233. index = i;
  234. break;
  235. }
  236. if (index < 0)
  237. return -EINVAL;
  238. ret = i2c_smbus_write_byte_data(data->client,
  239. STK8BA50_REG_RANGE,
  240. stk8ba50_scale_table[index].reg_val);
  241. if (ret < 0)
  242. dev_err(&data->client->dev,
  243. "failed to set measurement range\n");
  244. else
  245. data->range = index;
  246. return ret;
  247. case IIO_CHAN_INFO_SAMP_FREQ:
  248. for (i = 0; i < ARRAY_SIZE(stk8ba50_samp_freq_table); i++)
  249. if (val == stk8ba50_samp_freq_table[i].samp_freq) {
  250. index = i;
  251. break;
  252. }
  253. if (index < 0)
  254. return -EINVAL;
  255. ret = i2c_smbus_write_byte_data(data->client,
  256. STK8BA50_REG_BWSEL,
  257. stk8ba50_samp_freq_table[index].reg_val);
  258. if (ret < 0)
  259. dev_err(&data->client->dev,
  260. "failed to set sampling rate\n");
  261. else
  262. data->sample_rate_idx = index;
  263. return ret;
  264. }
  265. return -EINVAL;
  266. }
  267. static const struct iio_info stk8ba50_info = {
  268. .driver_module = THIS_MODULE,
  269. .read_raw = stk8ba50_read_raw,
  270. .write_raw = stk8ba50_write_raw,
  271. .attrs = &stk8ba50_attribute_group,
  272. };
  273. static irqreturn_t stk8ba50_trigger_handler(int irq, void *p)
  274. {
  275. struct iio_poll_func *pf = p;
  276. struct iio_dev *indio_dev = pf->indio_dev;
  277. struct stk8ba50_data *data = iio_priv(indio_dev);
  278. int bit, ret, i = 0;
  279. mutex_lock(&data->lock);
  280. /*
  281. * Do a bulk read if all channels are requested,
  282. * from 0x02 (XOUT1) to 0x07 (ZOUT2)
  283. */
  284. if (*(indio_dev->active_scan_mask) == STK8BA50_ALL_CHANNEL_MASK) {
  285. ret = i2c_smbus_read_i2c_block_data(data->client,
  286. STK8BA50_REG_XOUT,
  287. STK8BA50_ALL_CHANNEL_SIZE,
  288. (u8 *)data->scan.chans);
  289. if (ret < STK8BA50_ALL_CHANNEL_SIZE) {
  290. dev_err(&data->client->dev, "register read failed\n");
  291. goto err;
  292. }
  293. } else {
  294. for_each_set_bit(bit, indio_dev->active_scan_mask,
  295. indio_dev->masklength) {
  296. ret = stk8ba50_read_accel(data,
  297. stk8ba50_channel_table[bit]);
  298. if (ret < 0)
  299. goto err;
  300. data->scan.chans[i++] = ret;
  301. }
  302. }
  303. iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
  304. pf->timestamp);
  305. err:
  306. mutex_unlock(&data->lock);
  307. iio_trigger_notify_done(indio_dev->trig);
  308. return IRQ_HANDLED;
  309. }
  310. static irqreturn_t stk8ba50_data_rdy_trig_poll(int irq, void *private)
  311. {
  312. struct iio_dev *indio_dev = private;
  313. struct stk8ba50_data *data = iio_priv(indio_dev);
  314. if (data->dready_trigger_on)
  315. iio_trigger_poll(data->dready_trig);
  316. return IRQ_HANDLED;
  317. }
  318. static int stk8ba50_buffer_preenable(struct iio_dev *indio_dev)
  319. {
  320. struct stk8ba50_data *data = iio_priv(indio_dev);
  321. return stk8ba50_set_power(data, STK8BA50_MODE_NORMAL);
  322. }
  323. static int stk8ba50_buffer_postdisable(struct iio_dev *indio_dev)
  324. {
  325. struct stk8ba50_data *data = iio_priv(indio_dev);
  326. return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
  327. }
  328. static const struct iio_buffer_setup_ops stk8ba50_buffer_setup_ops = {
  329. .preenable = stk8ba50_buffer_preenable,
  330. .postenable = iio_triggered_buffer_postenable,
  331. .predisable = iio_triggered_buffer_predisable,
  332. .postdisable = stk8ba50_buffer_postdisable,
  333. };
  334. static int stk8ba50_probe(struct i2c_client *client,
  335. const struct i2c_device_id *id)
  336. {
  337. int ret;
  338. struct iio_dev *indio_dev;
  339. struct stk8ba50_data *data;
  340. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  341. if (!indio_dev) {
  342. dev_err(&client->dev, "iio allocation failed!\n");
  343. return -ENOMEM;
  344. }
  345. data = iio_priv(indio_dev);
  346. data->client = client;
  347. i2c_set_clientdata(client, indio_dev);
  348. mutex_init(&data->lock);
  349. indio_dev->dev.parent = &client->dev;
  350. indio_dev->info = &stk8ba50_info;
  351. indio_dev->name = STK8BA50_DRIVER_NAME;
  352. indio_dev->modes = INDIO_DIRECT_MODE;
  353. indio_dev->channels = stk8ba50_channels;
  354. indio_dev->num_channels = ARRAY_SIZE(stk8ba50_channels);
  355. /* Reset all registers on startup */
  356. ret = i2c_smbus_write_byte_data(client,
  357. STK8BA50_REG_SWRST, STK8BA50_RESET_CMD);
  358. if (ret < 0) {
  359. dev_err(&client->dev, "failed to reset sensor\n");
  360. goto err_power_off;
  361. }
  362. /* The default range is +/-2g */
  363. data->range = 0;
  364. /* The default sampling rate is 1792 Hz (maximum) */
  365. data->sample_rate_idx = STK8BA50_SR_1792HZ_IDX;
  366. /* Set up interrupts */
  367. ret = i2c_smbus_write_byte_data(client,
  368. STK8BA50_REG_INTEN2, STK8BA50_DREADY_INT_MASK);
  369. if (ret < 0) {
  370. dev_err(&client->dev, "failed to set up interrupts\n");
  371. goto err_power_off;
  372. }
  373. ret = i2c_smbus_write_byte_data(client,
  374. STK8BA50_REG_INTMAP2, STK8BA50_DREADY_INT_MAP);
  375. if (ret < 0) {
  376. dev_err(&client->dev, "failed to set up interrupts\n");
  377. goto err_power_off;
  378. }
  379. if (client->irq > 0) {
  380. ret = devm_request_threaded_irq(&client->dev, client->irq,
  381. stk8ba50_data_rdy_trig_poll,
  382. NULL,
  383. IRQF_TRIGGER_RISING |
  384. IRQF_ONESHOT,
  385. STK8BA50_IRQ_NAME,
  386. indio_dev);
  387. if (ret < 0) {
  388. dev_err(&client->dev, "request irq %d failed\n",
  389. client->irq);
  390. goto err_power_off;
  391. }
  392. data->dready_trig = devm_iio_trigger_alloc(&client->dev,
  393. "%s-dev%d",
  394. indio_dev->name,
  395. indio_dev->id);
  396. if (!data->dready_trig) {
  397. ret = -ENOMEM;
  398. goto err_power_off;
  399. }
  400. data->dready_trig->dev.parent = &client->dev;
  401. data->dready_trig->ops = &stk8ba50_trigger_ops;
  402. iio_trigger_set_drvdata(data->dready_trig, indio_dev);
  403. ret = iio_trigger_register(data->dready_trig);
  404. if (ret) {
  405. dev_err(&client->dev, "iio trigger register failed\n");
  406. goto err_power_off;
  407. }
  408. }
  409. ret = iio_triggered_buffer_setup(indio_dev,
  410. iio_pollfunc_store_time,
  411. stk8ba50_trigger_handler,
  412. &stk8ba50_buffer_setup_ops);
  413. if (ret < 0) {
  414. dev_err(&client->dev, "iio triggered buffer setup failed\n");
  415. goto err_trigger_unregister;
  416. }
  417. ret = iio_device_register(indio_dev);
  418. if (ret < 0) {
  419. dev_err(&client->dev, "device_register failed\n");
  420. goto err_buffer_cleanup;
  421. }
  422. return ret;
  423. err_buffer_cleanup:
  424. iio_triggered_buffer_cleanup(indio_dev);
  425. err_trigger_unregister:
  426. if (data->dready_trig)
  427. iio_trigger_unregister(data->dready_trig);
  428. err_power_off:
  429. stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
  430. return ret;
  431. }
  432. static int stk8ba50_remove(struct i2c_client *client)
  433. {
  434. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  435. struct stk8ba50_data *data = iio_priv(indio_dev);
  436. iio_device_unregister(indio_dev);
  437. iio_triggered_buffer_cleanup(indio_dev);
  438. if (data->dready_trig)
  439. iio_trigger_unregister(data->dready_trig);
  440. return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
  441. }
  442. #ifdef CONFIG_PM_SLEEP
  443. static int stk8ba50_suspend(struct device *dev)
  444. {
  445. struct stk8ba50_data *data;
  446. data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
  447. return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
  448. }
  449. static int stk8ba50_resume(struct device *dev)
  450. {
  451. struct stk8ba50_data *data;
  452. data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
  453. return stk8ba50_set_power(data, STK8BA50_MODE_NORMAL);
  454. }
  455. static SIMPLE_DEV_PM_OPS(stk8ba50_pm_ops, stk8ba50_suspend, stk8ba50_resume);
  456. #define STK8BA50_PM_OPS (&stk8ba50_pm_ops)
  457. #else
  458. #define STK8BA50_PM_OPS NULL
  459. #endif
  460. static const struct i2c_device_id stk8ba50_i2c_id[] = {
  461. {"stk8ba50", 0},
  462. {}
  463. };
  464. MODULE_DEVICE_TABLE(i2c, stk8ba50_i2c_id);
  465. static const struct acpi_device_id stk8ba50_acpi_id[] = {
  466. {"STK8BA50", 0},
  467. {}
  468. };
  469. MODULE_DEVICE_TABLE(acpi, stk8ba50_acpi_id);
  470. static struct i2c_driver stk8ba50_driver = {
  471. .driver = {
  472. .name = "stk8ba50",
  473. .pm = STK8BA50_PM_OPS,
  474. .acpi_match_table = ACPI_PTR(stk8ba50_acpi_id),
  475. },
  476. .probe = stk8ba50_probe,
  477. .remove = stk8ba50_remove,
  478. .id_table = stk8ba50_i2c_id,
  479. };
  480. module_i2c_driver(stk8ba50_driver);
  481. MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
  482. MODULE_DESCRIPTION("STK8BA50 3-Axis Accelerometer driver");
  483. MODULE_LICENSE("GPL v2");