stk8ba50.c 14 KB

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