iqs624-pos.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Azoteq IQS624/625 Angular Position Sensors
  4. *
  5. * Copyright (C) 2019 Jeff LaBundy <[email protected]>
  6. */
  7. #include <linux/device.h>
  8. #include <linux/iio/events.h>
  9. #include <linux/iio/iio.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mfd/iqs62x.h>
  12. #include <linux/module.h>
  13. #include <linux/mutex.h>
  14. #include <linux/notifier.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regmap.h>
  17. #define IQS624_POS_DEG_OUT 0x16
  18. #define IQS624_POS_SCALE1 (314159 / 180)
  19. #define IQS624_POS_SCALE2 100000
  20. struct iqs624_pos_private {
  21. struct iqs62x_core *iqs62x;
  22. struct iio_dev *indio_dev;
  23. struct notifier_block notifier;
  24. struct mutex lock;
  25. bool angle_en;
  26. u16 angle;
  27. };
  28. static int iqs624_pos_angle_en(struct iqs62x_core *iqs62x, bool angle_en)
  29. {
  30. unsigned int event_mask = IQS624_HALL_UI_WHL_EVENT;
  31. /*
  32. * The IQS625 reports angular position in the form of coarse intervals,
  33. * so only interval change events are unmasked. Conversely, the IQS624
  34. * reports angular position down to one degree of resolution, so wheel
  35. * movement events are unmasked instead.
  36. */
  37. if (iqs62x->dev_desc->prod_num == IQS625_PROD_NUM)
  38. event_mask = IQS624_HALL_UI_INT_EVENT;
  39. return regmap_update_bits(iqs62x->regmap, IQS624_HALL_UI, event_mask,
  40. angle_en ? 0 : 0xFF);
  41. }
  42. static int iqs624_pos_notifier(struct notifier_block *notifier,
  43. unsigned long event_flags, void *context)
  44. {
  45. struct iqs62x_event_data *event_data = context;
  46. struct iqs624_pos_private *iqs624_pos;
  47. struct iqs62x_core *iqs62x;
  48. struct iio_dev *indio_dev;
  49. u16 angle = event_data->ui_data;
  50. s64 timestamp;
  51. int ret;
  52. iqs624_pos = container_of(notifier, struct iqs624_pos_private,
  53. notifier);
  54. indio_dev = iqs624_pos->indio_dev;
  55. timestamp = iio_get_time_ns(indio_dev);
  56. iqs62x = iqs624_pos->iqs62x;
  57. if (iqs62x->dev_desc->prod_num == IQS625_PROD_NUM)
  58. angle = event_data->interval;
  59. mutex_lock(&iqs624_pos->lock);
  60. if (event_flags & BIT(IQS62X_EVENT_SYS_RESET)) {
  61. ret = iqs624_pos_angle_en(iqs62x, iqs624_pos->angle_en);
  62. if (ret) {
  63. dev_err(indio_dev->dev.parent,
  64. "Failed to re-initialize device: %d\n", ret);
  65. ret = NOTIFY_BAD;
  66. } else {
  67. ret = NOTIFY_OK;
  68. }
  69. } else if (iqs624_pos->angle_en && (angle != iqs624_pos->angle)) {
  70. iio_push_event(indio_dev,
  71. IIO_UNMOD_EVENT_CODE(IIO_ANGL, 0,
  72. IIO_EV_TYPE_CHANGE,
  73. IIO_EV_DIR_NONE),
  74. timestamp);
  75. iqs624_pos->angle = angle;
  76. ret = NOTIFY_OK;
  77. } else {
  78. ret = NOTIFY_DONE;
  79. }
  80. mutex_unlock(&iqs624_pos->lock);
  81. return ret;
  82. }
  83. static void iqs624_pos_notifier_unregister(void *context)
  84. {
  85. struct iqs624_pos_private *iqs624_pos = context;
  86. struct iio_dev *indio_dev = iqs624_pos->indio_dev;
  87. int ret;
  88. ret = blocking_notifier_chain_unregister(&iqs624_pos->iqs62x->nh,
  89. &iqs624_pos->notifier);
  90. if (ret)
  91. dev_err(indio_dev->dev.parent,
  92. "Failed to unregister notifier: %d\n", ret);
  93. }
  94. static int iqs624_pos_angle_get(struct iqs62x_core *iqs62x, unsigned int *val)
  95. {
  96. int ret;
  97. __le16 val_buf;
  98. if (iqs62x->dev_desc->prod_num == IQS625_PROD_NUM)
  99. return regmap_read(iqs62x->regmap, iqs62x->dev_desc->interval,
  100. val);
  101. ret = regmap_raw_read(iqs62x->regmap, IQS624_POS_DEG_OUT, &val_buf,
  102. sizeof(val_buf));
  103. if (ret)
  104. return ret;
  105. *val = le16_to_cpu(val_buf);
  106. return 0;
  107. }
  108. static int iqs624_pos_read_raw(struct iio_dev *indio_dev,
  109. struct iio_chan_spec const *chan,
  110. int *val, int *val2, long mask)
  111. {
  112. struct iqs624_pos_private *iqs624_pos = iio_priv(indio_dev);
  113. struct iqs62x_core *iqs62x = iqs624_pos->iqs62x;
  114. unsigned int scale = 1;
  115. int ret;
  116. switch (mask) {
  117. case IIO_CHAN_INFO_RAW:
  118. ret = iqs624_pos_angle_get(iqs62x, val);
  119. if (ret)
  120. return ret;
  121. return IIO_VAL_INT;
  122. case IIO_CHAN_INFO_SCALE:
  123. if (iqs62x->dev_desc->prod_num == IQS625_PROD_NUM) {
  124. ret = regmap_read(iqs62x->regmap, IQS624_INTERVAL_DIV,
  125. &scale);
  126. if (ret)
  127. return ret;
  128. }
  129. *val = scale * IQS624_POS_SCALE1;
  130. *val2 = IQS624_POS_SCALE2;
  131. return IIO_VAL_FRACTIONAL;
  132. default:
  133. return -EINVAL;
  134. }
  135. }
  136. static int iqs624_pos_read_event_config(struct iio_dev *indio_dev,
  137. const struct iio_chan_spec *chan,
  138. enum iio_event_type type,
  139. enum iio_event_direction dir)
  140. {
  141. struct iqs624_pos_private *iqs624_pos = iio_priv(indio_dev);
  142. int ret;
  143. mutex_lock(&iqs624_pos->lock);
  144. ret = iqs624_pos->angle_en;
  145. mutex_unlock(&iqs624_pos->lock);
  146. return ret;
  147. }
  148. static int iqs624_pos_write_event_config(struct iio_dev *indio_dev,
  149. const struct iio_chan_spec *chan,
  150. enum iio_event_type type,
  151. enum iio_event_direction dir,
  152. int state)
  153. {
  154. struct iqs624_pos_private *iqs624_pos = iio_priv(indio_dev);
  155. struct iqs62x_core *iqs62x = iqs624_pos->iqs62x;
  156. unsigned int val;
  157. int ret;
  158. mutex_lock(&iqs624_pos->lock);
  159. ret = iqs624_pos_angle_get(iqs62x, &val);
  160. if (ret)
  161. goto err_mutex;
  162. ret = iqs624_pos_angle_en(iqs62x, state);
  163. if (ret)
  164. goto err_mutex;
  165. iqs624_pos->angle = val;
  166. iqs624_pos->angle_en = state;
  167. err_mutex:
  168. mutex_unlock(&iqs624_pos->lock);
  169. return ret;
  170. }
  171. static const struct iio_info iqs624_pos_info = {
  172. .read_raw = &iqs624_pos_read_raw,
  173. .read_event_config = iqs624_pos_read_event_config,
  174. .write_event_config = iqs624_pos_write_event_config,
  175. };
  176. static const struct iio_event_spec iqs624_pos_events[] = {
  177. {
  178. .type = IIO_EV_TYPE_CHANGE,
  179. .dir = IIO_EV_DIR_NONE,
  180. .mask_separate = BIT(IIO_EV_INFO_ENABLE),
  181. },
  182. };
  183. static const struct iio_chan_spec iqs624_pos_channels[] = {
  184. {
  185. .type = IIO_ANGL,
  186. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  187. BIT(IIO_CHAN_INFO_SCALE),
  188. .event_spec = iqs624_pos_events,
  189. .num_event_specs = ARRAY_SIZE(iqs624_pos_events),
  190. },
  191. };
  192. static int iqs624_pos_probe(struct platform_device *pdev)
  193. {
  194. struct iqs62x_core *iqs62x = dev_get_drvdata(pdev->dev.parent);
  195. struct iqs624_pos_private *iqs624_pos;
  196. struct iio_dev *indio_dev;
  197. int ret;
  198. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*iqs624_pos));
  199. if (!indio_dev)
  200. return -ENOMEM;
  201. iqs624_pos = iio_priv(indio_dev);
  202. iqs624_pos->iqs62x = iqs62x;
  203. iqs624_pos->indio_dev = indio_dev;
  204. indio_dev->modes = INDIO_DIRECT_MODE;
  205. indio_dev->channels = iqs624_pos_channels;
  206. indio_dev->num_channels = ARRAY_SIZE(iqs624_pos_channels);
  207. indio_dev->name = iqs62x->dev_desc->dev_name;
  208. indio_dev->info = &iqs624_pos_info;
  209. mutex_init(&iqs624_pos->lock);
  210. iqs624_pos->notifier.notifier_call = iqs624_pos_notifier;
  211. ret = blocking_notifier_chain_register(&iqs624_pos->iqs62x->nh,
  212. &iqs624_pos->notifier);
  213. if (ret) {
  214. dev_err(&pdev->dev, "Failed to register notifier: %d\n", ret);
  215. return ret;
  216. }
  217. ret = devm_add_action_or_reset(&pdev->dev,
  218. iqs624_pos_notifier_unregister,
  219. iqs624_pos);
  220. if (ret)
  221. return ret;
  222. return devm_iio_device_register(&pdev->dev, indio_dev);
  223. }
  224. static struct platform_driver iqs624_pos_platform_driver = {
  225. .driver = {
  226. .name = "iqs624-pos",
  227. },
  228. .probe = iqs624_pos_probe,
  229. };
  230. module_platform_driver(iqs624_pos_platform_driver);
  231. MODULE_AUTHOR("Jeff LaBundy <[email protected]>");
  232. MODULE_DESCRIPTION("Azoteq IQS624/625 Angular Position Sensors");
  233. MODULE_LICENSE("GPL");
  234. MODULE_ALIAS("platform:iqs624-pos");