hid-sensor-als.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * HID Sensors Driver
  3. * Copyright (c) 2012, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. */
  19. #include <linux/device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/module.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/irq.h>
  24. #include <linux/slab.h>
  25. #include <linux/hid-sensor-hub.h>
  26. #include <linux/iio/iio.h>
  27. #include <linux/iio/sysfs.h>
  28. #include <linux/iio/buffer.h>
  29. #include <linux/iio/trigger_consumer.h>
  30. #include <linux/iio/triggered_buffer.h>
  31. #include "../common/hid-sensors/hid-sensor-trigger.h"
  32. #define CHANNEL_SCAN_INDEX_ILLUM 0
  33. struct als_state {
  34. struct hid_sensor_hub_callbacks callbacks;
  35. struct hid_sensor_common common_attributes;
  36. struct hid_sensor_hub_attribute_info als_illum;
  37. u32 illum;
  38. };
  39. /* Channel definitions */
  40. static const struct iio_chan_spec als_channels[] = {
  41. {
  42. .type = IIO_INTENSITY,
  43. .modified = 1,
  44. .channel2 = IIO_MOD_LIGHT_BOTH,
  45. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  46. BIT(IIO_CHAN_INFO_SCALE) |
  47. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  48. BIT(IIO_CHAN_INFO_HYSTERESIS),
  49. .scan_index = CHANNEL_SCAN_INDEX_ILLUM,
  50. }
  51. };
  52. /* Adjust channel real bits based on report descriptor */
  53. static void als_adjust_channel_bit_mask(struct iio_chan_spec *channels,
  54. int channel, int size)
  55. {
  56. channels[channel].scan_type.sign = 's';
  57. /* Real storage bits will change based on the report desc. */
  58. channels[channel].scan_type.realbits = size * 8;
  59. /* Maximum size of a sample to capture is u32 */
  60. channels[channel].scan_type.storagebits = sizeof(u32) * 8;
  61. }
  62. /* Channel read_raw handler */
  63. static int als_read_raw(struct iio_dev *indio_dev,
  64. struct iio_chan_spec const *chan,
  65. int *val, int *val2,
  66. long mask)
  67. {
  68. struct als_state *als_state = iio_priv(indio_dev);
  69. int report_id = -1;
  70. u32 address;
  71. int ret;
  72. int ret_type;
  73. *val = 0;
  74. *val2 = 0;
  75. switch (mask) {
  76. case 0:
  77. switch (chan->scan_index) {
  78. case CHANNEL_SCAN_INDEX_ILLUM:
  79. report_id = als_state->als_illum.report_id;
  80. address =
  81. HID_USAGE_SENSOR_LIGHT_ILLUM;
  82. break;
  83. default:
  84. report_id = -1;
  85. break;
  86. }
  87. if (report_id >= 0)
  88. *val = sensor_hub_input_attr_get_raw_value(
  89. als_state->common_attributes.hsdev,
  90. HID_USAGE_SENSOR_ALS, address,
  91. report_id);
  92. else {
  93. *val = 0;
  94. return -EINVAL;
  95. }
  96. ret_type = IIO_VAL_INT;
  97. break;
  98. case IIO_CHAN_INFO_SCALE:
  99. *val = als_state->als_illum.units;
  100. ret_type = IIO_VAL_INT;
  101. break;
  102. case IIO_CHAN_INFO_OFFSET:
  103. *val = hid_sensor_convert_exponent(
  104. als_state->als_illum.unit_expo);
  105. ret_type = IIO_VAL_INT;
  106. break;
  107. case IIO_CHAN_INFO_SAMP_FREQ:
  108. ret = hid_sensor_read_samp_freq_value(
  109. &als_state->common_attributes, val, val2);
  110. ret_type = IIO_VAL_INT_PLUS_MICRO;
  111. break;
  112. case IIO_CHAN_INFO_HYSTERESIS:
  113. ret = hid_sensor_read_raw_hyst_value(
  114. &als_state->common_attributes, val, val2);
  115. ret_type = IIO_VAL_INT_PLUS_MICRO;
  116. break;
  117. default:
  118. ret_type = -EINVAL;
  119. break;
  120. }
  121. return ret_type;
  122. }
  123. /* Channel write_raw handler */
  124. static int als_write_raw(struct iio_dev *indio_dev,
  125. struct iio_chan_spec const *chan,
  126. int val,
  127. int val2,
  128. long mask)
  129. {
  130. struct als_state *als_state = iio_priv(indio_dev);
  131. int ret = 0;
  132. switch (mask) {
  133. case IIO_CHAN_INFO_SAMP_FREQ:
  134. ret = hid_sensor_write_samp_freq_value(
  135. &als_state->common_attributes, val, val2);
  136. break;
  137. case IIO_CHAN_INFO_HYSTERESIS:
  138. ret = hid_sensor_write_raw_hyst_value(
  139. &als_state->common_attributes, val, val2);
  140. break;
  141. default:
  142. ret = -EINVAL;
  143. }
  144. return ret;
  145. }
  146. static const struct iio_info als_info = {
  147. .driver_module = THIS_MODULE,
  148. .read_raw = &als_read_raw,
  149. .write_raw = &als_write_raw,
  150. };
  151. /* Function to push data to buffer */
  152. static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
  153. int len)
  154. {
  155. dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
  156. iio_push_to_buffers(indio_dev, data);
  157. }
  158. /* Callback handler to send event after all samples are received and captured */
  159. static int als_proc_event(struct hid_sensor_hub_device *hsdev,
  160. unsigned usage_id,
  161. void *priv)
  162. {
  163. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  164. struct als_state *als_state = iio_priv(indio_dev);
  165. dev_dbg(&indio_dev->dev, "als_proc_event [%d]\n",
  166. als_state->common_attributes.data_ready);
  167. if (als_state->common_attributes.data_ready)
  168. hid_sensor_push_data(indio_dev,
  169. &als_state->illum,
  170. sizeof(als_state->illum));
  171. return 0;
  172. }
  173. /* Capture samples in local storage */
  174. static int als_capture_sample(struct hid_sensor_hub_device *hsdev,
  175. unsigned usage_id,
  176. size_t raw_len, char *raw_data,
  177. void *priv)
  178. {
  179. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  180. struct als_state *als_state = iio_priv(indio_dev);
  181. int ret = -EINVAL;
  182. switch (usage_id) {
  183. case HID_USAGE_SENSOR_LIGHT_ILLUM:
  184. als_state->illum = *(u32 *)raw_data;
  185. ret = 0;
  186. break;
  187. default:
  188. break;
  189. }
  190. return ret;
  191. }
  192. /* Parse report which is specific to an usage id*/
  193. static int als_parse_report(struct platform_device *pdev,
  194. struct hid_sensor_hub_device *hsdev,
  195. struct iio_chan_spec *channels,
  196. unsigned usage_id,
  197. struct als_state *st)
  198. {
  199. int ret;
  200. ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
  201. usage_id,
  202. HID_USAGE_SENSOR_LIGHT_ILLUM,
  203. &st->als_illum);
  204. if (ret < 0)
  205. return ret;
  206. als_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_ILLUM,
  207. st->als_illum.size);
  208. dev_dbg(&pdev->dev, "als %x:%x\n", st->als_illum.index,
  209. st->als_illum.report_id);
  210. return ret;
  211. }
  212. /* Function to initialize the processing for usage id */
  213. static int hid_als_probe(struct platform_device *pdev)
  214. {
  215. int ret = 0;
  216. static const char *name = "als";
  217. struct iio_dev *indio_dev;
  218. struct als_state *als_state;
  219. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  220. struct iio_chan_spec *channels;
  221. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct als_state));
  222. if (!indio_dev)
  223. return -ENOMEM;
  224. platform_set_drvdata(pdev, indio_dev);
  225. als_state = iio_priv(indio_dev);
  226. als_state->common_attributes.hsdev = hsdev;
  227. als_state->common_attributes.pdev = pdev;
  228. ret = hid_sensor_parse_common_attributes(hsdev, HID_USAGE_SENSOR_ALS,
  229. &als_state->common_attributes);
  230. if (ret) {
  231. dev_err(&pdev->dev, "failed to setup common attributes\n");
  232. return ret;
  233. }
  234. channels = kmemdup(als_channels, sizeof(als_channels), GFP_KERNEL);
  235. if (!channels) {
  236. dev_err(&pdev->dev, "failed to duplicate channels\n");
  237. return -ENOMEM;
  238. }
  239. ret = als_parse_report(pdev, hsdev, channels,
  240. HID_USAGE_SENSOR_ALS, als_state);
  241. if (ret) {
  242. dev_err(&pdev->dev, "failed to setup attributes\n");
  243. goto error_free_dev_mem;
  244. }
  245. indio_dev->channels = channels;
  246. indio_dev->num_channels =
  247. ARRAY_SIZE(als_channels);
  248. indio_dev->dev.parent = &pdev->dev;
  249. indio_dev->info = &als_info;
  250. indio_dev->name = name;
  251. indio_dev->modes = INDIO_DIRECT_MODE;
  252. ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  253. NULL, NULL);
  254. if (ret) {
  255. dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
  256. goto error_free_dev_mem;
  257. }
  258. als_state->common_attributes.data_ready = false;
  259. ret = hid_sensor_setup_trigger(indio_dev, name,
  260. &als_state->common_attributes);
  261. if (ret < 0) {
  262. dev_err(&pdev->dev, "trigger setup failed\n");
  263. goto error_unreg_buffer_funcs;
  264. }
  265. ret = iio_device_register(indio_dev);
  266. if (ret) {
  267. dev_err(&pdev->dev, "device register failed\n");
  268. goto error_remove_trigger;
  269. }
  270. als_state->callbacks.send_event = als_proc_event;
  271. als_state->callbacks.capture_sample = als_capture_sample;
  272. als_state->callbacks.pdev = pdev;
  273. ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_ALS,
  274. &als_state->callbacks);
  275. if (ret < 0) {
  276. dev_err(&pdev->dev, "callback reg failed\n");
  277. goto error_iio_unreg;
  278. }
  279. return ret;
  280. error_iio_unreg:
  281. iio_device_unregister(indio_dev);
  282. error_remove_trigger:
  283. hid_sensor_remove_trigger(&als_state->common_attributes);
  284. error_unreg_buffer_funcs:
  285. iio_triggered_buffer_cleanup(indio_dev);
  286. error_free_dev_mem:
  287. kfree(indio_dev->channels);
  288. return ret;
  289. }
  290. /* Function to deinitialize the processing for usage id */
  291. static int hid_als_remove(struct platform_device *pdev)
  292. {
  293. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  294. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  295. struct als_state *als_state = iio_priv(indio_dev);
  296. sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_ALS);
  297. iio_device_unregister(indio_dev);
  298. hid_sensor_remove_trigger(&als_state->common_attributes);
  299. iio_triggered_buffer_cleanup(indio_dev);
  300. kfree(indio_dev->channels);
  301. return 0;
  302. }
  303. static struct platform_device_id hid_als_ids[] = {
  304. {
  305. /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
  306. .name = "HID-SENSOR-200041",
  307. },
  308. { /* sentinel */ }
  309. };
  310. MODULE_DEVICE_TABLE(platform, hid_als_ids);
  311. static struct platform_driver hid_als_platform_driver = {
  312. .id_table = hid_als_ids,
  313. .driver = {
  314. .name = KBUILD_MODNAME,
  315. .owner = THIS_MODULE,
  316. },
  317. .probe = hid_als_probe,
  318. .remove = hid_als_remove,
  319. };
  320. module_platform_driver(hid_als_platform_driver);
  321. MODULE_DESCRIPTION("HID Sensor ALS");
  322. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
  323. MODULE_LICENSE("GPL");