mxc4005.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * 3-axis accelerometer driver for MXC4005XC Memsic sensor
  3. *
  4. * Copyright (c) 2014, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/i2c.h>
  17. #include <linux/iio/iio.h>
  18. #include <linux/acpi.h>
  19. #include <linux/regmap.h>
  20. #include <linux/iio/sysfs.h>
  21. #include <linux/iio/trigger.h>
  22. #include <linux/iio/buffer.h>
  23. #include <linux/iio/triggered_buffer.h>
  24. #include <linux/iio/trigger_consumer.h>
  25. #define MXC4005_DRV_NAME "mxc4005"
  26. #define MXC4005_IRQ_NAME "mxc4005_event"
  27. #define MXC4005_REGMAP_NAME "mxc4005_regmap"
  28. #define MXC4005_REG_XOUT_UPPER 0x03
  29. #define MXC4005_REG_XOUT_LOWER 0x04
  30. #define MXC4005_REG_YOUT_UPPER 0x05
  31. #define MXC4005_REG_YOUT_LOWER 0x06
  32. #define MXC4005_REG_ZOUT_UPPER 0x07
  33. #define MXC4005_REG_ZOUT_LOWER 0x08
  34. #define MXC4005_REG_INT_MASK1 0x0B
  35. #define MXC4005_REG_INT_MASK1_BIT_DRDYE 0x01
  36. #define MXC4005_REG_INT_CLR1 0x01
  37. #define MXC4005_REG_INT_CLR1_BIT_DRDYC 0x01
  38. #define MXC4005_REG_CONTROL 0x0D
  39. #define MXC4005_REG_CONTROL_MASK_FSR GENMASK(6, 5)
  40. #define MXC4005_CONTROL_FSR_SHIFT 5
  41. #define MXC4005_REG_DEVICE_ID 0x0E
  42. enum mxc4005_axis {
  43. AXIS_X,
  44. AXIS_Y,
  45. AXIS_Z,
  46. };
  47. enum mxc4005_range {
  48. MXC4005_RANGE_2G,
  49. MXC4005_RANGE_4G,
  50. MXC4005_RANGE_8G,
  51. };
  52. struct mxc4005_data {
  53. struct device *dev;
  54. struct mutex mutex;
  55. struct regmap *regmap;
  56. struct iio_trigger *dready_trig;
  57. __be16 buffer[8];
  58. bool trigger_enabled;
  59. };
  60. /*
  61. * MXC4005 can operate in the following ranges:
  62. * +/- 2G, 4G, 8G (the default +/-2G)
  63. *
  64. * (2 + 2) * 9.81 / (2^12 - 1) = 0.009582
  65. * (4 + 4) * 9.81 / (2^12 - 1) = 0.019164
  66. * (8 + 8) * 9.81 / (2^12 - 1) = 0.038329
  67. */
  68. static const struct {
  69. u8 range;
  70. int scale;
  71. } mxc4005_scale_table[] = {
  72. {MXC4005_RANGE_2G, 9582},
  73. {MXC4005_RANGE_4G, 19164},
  74. {MXC4005_RANGE_8G, 38329},
  75. };
  76. static IIO_CONST_ATTR(in_accel_scale_available, "0.009582 0.019164 0.038329");
  77. static struct attribute *mxc4005_attributes[] = {
  78. &iio_const_attr_in_accel_scale_available.dev_attr.attr,
  79. NULL,
  80. };
  81. static const struct attribute_group mxc4005_attrs_group = {
  82. .attrs = mxc4005_attributes,
  83. };
  84. static bool mxc4005_is_readable_reg(struct device *dev, unsigned int reg)
  85. {
  86. switch (reg) {
  87. case MXC4005_REG_XOUT_UPPER:
  88. case MXC4005_REG_XOUT_LOWER:
  89. case MXC4005_REG_YOUT_UPPER:
  90. case MXC4005_REG_YOUT_LOWER:
  91. case MXC4005_REG_ZOUT_UPPER:
  92. case MXC4005_REG_ZOUT_LOWER:
  93. case MXC4005_REG_DEVICE_ID:
  94. case MXC4005_REG_CONTROL:
  95. return true;
  96. default:
  97. return false;
  98. }
  99. }
  100. static bool mxc4005_is_writeable_reg(struct device *dev, unsigned int reg)
  101. {
  102. switch (reg) {
  103. case MXC4005_REG_INT_CLR1:
  104. case MXC4005_REG_INT_MASK1:
  105. case MXC4005_REG_CONTROL:
  106. return true;
  107. default:
  108. return false;
  109. }
  110. }
  111. static const struct regmap_config mxc4005_regmap_config = {
  112. .name = MXC4005_REGMAP_NAME,
  113. .reg_bits = 8,
  114. .val_bits = 8,
  115. .max_register = MXC4005_REG_DEVICE_ID,
  116. .readable_reg = mxc4005_is_readable_reg,
  117. .writeable_reg = mxc4005_is_writeable_reg,
  118. };
  119. static int mxc4005_read_xyz(struct mxc4005_data *data)
  120. {
  121. int ret;
  122. ret = regmap_bulk_read(data->regmap, MXC4005_REG_XOUT_UPPER,
  123. (u8 *) data->buffer, sizeof(data->buffer));
  124. if (ret < 0) {
  125. dev_err(data->dev, "failed to read axes\n");
  126. return ret;
  127. }
  128. return 0;
  129. }
  130. static int mxc4005_read_axis(struct mxc4005_data *data,
  131. unsigned int addr)
  132. {
  133. __be16 reg;
  134. int ret;
  135. ret = regmap_bulk_read(data->regmap, addr, (u8 *) &reg, sizeof(reg));
  136. if (ret < 0) {
  137. dev_err(data->dev, "failed to read reg %02x\n", addr);
  138. return ret;
  139. }
  140. return be16_to_cpu(reg);
  141. }
  142. static int mxc4005_read_scale(struct mxc4005_data *data)
  143. {
  144. unsigned int reg;
  145. int ret;
  146. int i;
  147. ret = regmap_read(data->regmap, MXC4005_REG_CONTROL, &reg);
  148. if (ret < 0) {
  149. dev_err(data->dev, "failed to read reg_control\n");
  150. return ret;
  151. }
  152. i = reg >> MXC4005_CONTROL_FSR_SHIFT;
  153. if (i < 0 || i >= ARRAY_SIZE(mxc4005_scale_table))
  154. return -EINVAL;
  155. return mxc4005_scale_table[i].scale;
  156. }
  157. static int mxc4005_set_scale(struct mxc4005_data *data, int val)
  158. {
  159. unsigned int reg;
  160. int i;
  161. int ret;
  162. for (i = 0; i < ARRAY_SIZE(mxc4005_scale_table); i++) {
  163. if (mxc4005_scale_table[i].scale == val) {
  164. reg = i << MXC4005_CONTROL_FSR_SHIFT;
  165. ret = regmap_update_bits(data->regmap,
  166. MXC4005_REG_CONTROL,
  167. MXC4005_REG_CONTROL_MASK_FSR,
  168. reg);
  169. if (ret < 0)
  170. dev_err(data->dev,
  171. "failed to write reg_control\n");
  172. return ret;
  173. }
  174. }
  175. return -EINVAL;
  176. }
  177. static int mxc4005_read_raw(struct iio_dev *indio_dev,
  178. struct iio_chan_spec const *chan,
  179. int *val, int *val2, long mask)
  180. {
  181. struct mxc4005_data *data = iio_priv(indio_dev);
  182. int ret;
  183. switch (mask) {
  184. case IIO_CHAN_INFO_RAW:
  185. switch (chan->type) {
  186. case IIO_ACCEL:
  187. if (iio_buffer_enabled(indio_dev))
  188. return -EBUSY;
  189. ret = mxc4005_read_axis(data, chan->address);
  190. if (ret < 0)
  191. return ret;
  192. *val = sign_extend32(ret >> chan->scan_type.shift,
  193. chan->scan_type.realbits - 1);
  194. return IIO_VAL_INT;
  195. default:
  196. return -EINVAL;
  197. }
  198. case IIO_CHAN_INFO_SCALE:
  199. ret = mxc4005_read_scale(data);
  200. if (ret < 0)
  201. return ret;
  202. *val = 0;
  203. *val2 = ret;
  204. return IIO_VAL_INT_PLUS_MICRO;
  205. default:
  206. return -EINVAL;
  207. }
  208. }
  209. static int mxc4005_write_raw(struct iio_dev *indio_dev,
  210. struct iio_chan_spec const *chan,
  211. int val, int val2, long mask)
  212. {
  213. struct mxc4005_data *data = iio_priv(indio_dev);
  214. switch (mask) {
  215. case IIO_CHAN_INFO_SCALE:
  216. if (val != 0)
  217. return -EINVAL;
  218. return mxc4005_set_scale(data, val2);
  219. default:
  220. return -EINVAL;
  221. }
  222. }
  223. static const struct iio_info mxc4005_info = {
  224. .driver_module = THIS_MODULE,
  225. .read_raw = mxc4005_read_raw,
  226. .write_raw = mxc4005_write_raw,
  227. .attrs = &mxc4005_attrs_group,
  228. };
  229. static const unsigned long mxc4005_scan_masks[] = {
  230. BIT(AXIS_X) | BIT(AXIS_Y) | BIT(AXIS_Z),
  231. 0
  232. };
  233. #define MXC4005_CHANNEL(_axis, _addr) { \
  234. .type = IIO_ACCEL, \
  235. .modified = 1, \
  236. .channel2 = IIO_MOD_##_axis, \
  237. .address = _addr, \
  238. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  239. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  240. .scan_index = AXIS_##_axis, \
  241. .scan_type = { \
  242. .sign = 's', \
  243. .realbits = 12, \
  244. .storagebits = 16, \
  245. .shift = 4, \
  246. .endianness = IIO_BE, \
  247. }, \
  248. }
  249. static const struct iio_chan_spec mxc4005_channels[] = {
  250. MXC4005_CHANNEL(X, MXC4005_REG_XOUT_UPPER),
  251. MXC4005_CHANNEL(Y, MXC4005_REG_YOUT_UPPER),
  252. MXC4005_CHANNEL(Z, MXC4005_REG_ZOUT_UPPER),
  253. IIO_CHAN_SOFT_TIMESTAMP(3),
  254. };
  255. static irqreturn_t mxc4005_trigger_handler(int irq, void *private)
  256. {
  257. struct iio_poll_func *pf = private;
  258. struct iio_dev *indio_dev = pf->indio_dev;
  259. struct mxc4005_data *data = iio_priv(indio_dev);
  260. int ret;
  261. ret = mxc4005_read_xyz(data);
  262. if (ret < 0)
  263. goto err;
  264. iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
  265. pf->timestamp);
  266. err:
  267. iio_trigger_notify_done(indio_dev->trig);
  268. return IRQ_HANDLED;
  269. }
  270. static int mxc4005_clr_intr(struct mxc4005_data *data)
  271. {
  272. int ret;
  273. /* clear interrupt */
  274. ret = regmap_write(data->regmap, MXC4005_REG_INT_CLR1,
  275. MXC4005_REG_INT_CLR1_BIT_DRDYC);
  276. if (ret < 0) {
  277. dev_err(data->dev, "failed to write to reg_int_clr1\n");
  278. return ret;
  279. }
  280. return 0;
  281. }
  282. static int mxc4005_set_trigger_state(struct iio_trigger *trig,
  283. bool state)
  284. {
  285. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  286. struct mxc4005_data *data = iio_priv(indio_dev);
  287. int ret;
  288. mutex_lock(&data->mutex);
  289. if (state) {
  290. ret = regmap_write(data->regmap, MXC4005_REG_INT_MASK1,
  291. MXC4005_REG_INT_MASK1_BIT_DRDYE);
  292. } else {
  293. ret = regmap_write(data->regmap, MXC4005_REG_INT_MASK1,
  294. ~MXC4005_REG_INT_MASK1_BIT_DRDYE);
  295. }
  296. if (ret < 0) {
  297. mutex_unlock(&data->mutex);
  298. dev_err(data->dev, "failed to update reg_int_mask1");
  299. return ret;
  300. }
  301. data->trigger_enabled = state;
  302. mutex_unlock(&data->mutex);
  303. return 0;
  304. }
  305. static int mxc4005_trigger_try_reen(struct iio_trigger *trig)
  306. {
  307. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  308. struct mxc4005_data *data = iio_priv(indio_dev);
  309. if (!data->dready_trig)
  310. return 0;
  311. return mxc4005_clr_intr(data);
  312. }
  313. static const struct iio_trigger_ops mxc4005_trigger_ops = {
  314. .set_trigger_state = mxc4005_set_trigger_state,
  315. .try_reenable = mxc4005_trigger_try_reen,
  316. .owner = THIS_MODULE,
  317. };
  318. static int mxc4005_chip_init(struct mxc4005_data *data)
  319. {
  320. int ret;
  321. unsigned int reg;
  322. ret = regmap_read(data->regmap, MXC4005_REG_DEVICE_ID, &reg);
  323. if (ret < 0) {
  324. dev_err(data->dev, "failed to read chip id\n");
  325. return ret;
  326. }
  327. dev_dbg(data->dev, "MXC4005 chip id %02x\n", reg);
  328. return 0;
  329. }
  330. static int mxc4005_probe(struct i2c_client *client,
  331. const struct i2c_device_id *id)
  332. {
  333. struct mxc4005_data *data;
  334. struct iio_dev *indio_dev;
  335. struct regmap *regmap;
  336. int ret;
  337. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  338. if (!indio_dev)
  339. return -ENOMEM;
  340. regmap = devm_regmap_init_i2c(client, &mxc4005_regmap_config);
  341. if (IS_ERR(regmap)) {
  342. dev_err(&client->dev, "failed to initialize regmap\n");
  343. return PTR_ERR(regmap);
  344. }
  345. data = iio_priv(indio_dev);
  346. i2c_set_clientdata(client, indio_dev);
  347. data->dev = &client->dev;
  348. data->regmap = regmap;
  349. ret = mxc4005_chip_init(data);
  350. if (ret < 0) {
  351. dev_err(&client->dev, "failed to initialize chip\n");
  352. return ret;
  353. }
  354. mutex_init(&data->mutex);
  355. indio_dev->dev.parent = &client->dev;
  356. indio_dev->channels = mxc4005_channels;
  357. indio_dev->num_channels = ARRAY_SIZE(mxc4005_channels);
  358. indio_dev->available_scan_masks = mxc4005_scan_masks;
  359. indio_dev->name = MXC4005_DRV_NAME;
  360. indio_dev->modes = INDIO_DIRECT_MODE;
  361. indio_dev->info = &mxc4005_info;
  362. ret = iio_triggered_buffer_setup(indio_dev,
  363. iio_pollfunc_store_time,
  364. mxc4005_trigger_handler,
  365. NULL);
  366. if (ret < 0) {
  367. dev_err(&client->dev,
  368. "failed to setup iio triggered buffer\n");
  369. return ret;
  370. }
  371. if (client->irq > 0) {
  372. data->dready_trig = devm_iio_trigger_alloc(&client->dev,
  373. "%s-dev%d",
  374. indio_dev->name,
  375. indio_dev->id);
  376. if (!data->dready_trig)
  377. return -ENOMEM;
  378. ret = devm_request_threaded_irq(&client->dev, client->irq,
  379. iio_trigger_generic_data_rdy_poll,
  380. NULL,
  381. IRQF_TRIGGER_FALLING |
  382. IRQF_ONESHOT,
  383. MXC4005_IRQ_NAME,
  384. data->dready_trig);
  385. if (ret) {
  386. dev_err(&client->dev,
  387. "failed to init threaded irq\n");
  388. goto err_buffer_cleanup;
  389. }
  390. data->dready_trig->dev.parent = &client->dev;
  391. data->dready_trig->ops = &mxc4005_trigger_ops;
  392. iio_trigger_set_drvdata(data->dready_trig, indio_dev);
  393. indio_dev->trig = data->dready_trig;
  394. iio_trigger_get(indio_dev->trig);
  395. ret = iio_trigger_register(data->dready_trig);
  396. if (ret) {
  397. dev_err(&client->dev,
  398. "failed to register trigger\n");
  399. goto err_trigger_unregister;
  400. }
  401. }
  402. ret = iio_device_register(indio_dev);
  403. if (ret < 0) {
  404. dev_err(&client->dev,
  405. "unable to register iio device %d\n", ret);
  406. goto err_buffer_cleanup;
  407. }
  408. return 0;
  409. err_trigger_unregister:
  410. iio_trigger_unregister(data->dready_trig);
  411. err_buffer_cleanup:
  412. iio_triggered_buffer_cleanup(indio_dev);
  413. return ret;
  414. }
  415. static int mxc4005_remove(struct i2c_client *client)
  416. {
  417. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  418. struct mxc4005_data *data = iio_priv(indio_dev);
  419. iio_device_unregister(indio_dev);
  420. iio_triggered_buffer_cleanup(indio_dev);
  421. if (data->dready_trig)
  422. iio_trigger_unregister(data->dready_trig);
  423. return 0;
  424. }
  425. static const struct acpi_device_id mxc4005_acpi_match[] = {
  426. {"MXC4005", 0},
  427. { },
  428. };
  429. MODULE_DEVICE_TABLE(acpi, mxc4005_acpi_match);
  430. static const struct i2c_device_id mxc4005_id[] = {
  431. {"mxc4005", 0},
  432. { },
  433. };
  434. MODULE_DEVICE_TABLE(i2c, mxc4005_id);
  435. static struct i2c_driver mxc4005_driver = {
  436. .driver = {
  437. .name = MXC4005_DRV_NAME,
  438. .acpi_match_table = ACPI_PTR(mxc4005_acpi_match),
  439. },
  440. .probe = mxc4005_probe,
  441. .remove = mxc4005_remove,
  442. .id_table = mxc4005_id,
  443. };
  444. module_i2c_driver(mxc4005_driver);
  445. MODULE_AUTHOR("Teodora Baluta <teodora.baluta@intel.com>");
  446. MODULE_LICENSE("GPL v2");
  447. MODULE_DESCRIPTION("MXC4005 3-axis accelerometer driver");